actually I want: if a user is authenticated: then create/get the Cart with user, else: create/get the Cart with session key. But at first problem happened with authentication. At first I tried to register the user and saved the key(got from drf) in local storage. in Reactjs: I think no problem here. if I console.log() the key , it prints
Tag: django-rest-framework
how to return nested json using django rest framework
I am trying build a REST API using django. here is my models.py and serializers.py. models.py serailizers.py Here is my api – http://127.0.0.1:8000/api/city/Sidney/. I am trying to fetch data by city name. I am getting the json in below format. But i want the json in below shown format – i am not getting what change needs to be done
How to cache individual Django REST API POSTs for bulk_create?
I have a Django REST API endpoint. It receives a JSON payload eg. This is decoded in a views.py function and generates a new database object like so (pseudo code): In tests, it is 20x faster to create a list of x1000 newobjs, then do a bulk create: So, the question is how to save individual POSTs somewhere in Django
Can’t login my users, they don’t seem to be registering properly
I’m trying to login the users I registered in my registration view (where I get a 200 code that says POST request was successful) but I get “Unable to log in with provided credentials.” when I try to log them in. When I check in the shell with User.objects.all(), I only get my superuser back so I think the users
Django: How to use data from a function as a field in Meta class
I’ve created a function in my serializers.py that call an external API and give me a dict back. How can I use the output from return downloads in the get_all_files as a field in class Meta? After the first answer, I’ve got the following error message: Line 68 is the following: return get_all_files(instance.bands) serializers.py Answer The code works fine. The
Creating django api view that returns filtered objects
Problem Description I’m new to django-rest-framework and got a problem in my view. I have three models namely #User, #Profession and #StudentProfessions. I need to create an api-view that takes user_id as request-parameter and returns a list of all professions that belongs to a particular user_id. Here’s My Codes profession_app >> models.py User Model I used the django default model.
How to return empty queryset from serializer.data Django Rest Framework
view.py serializer.py I want the serializer to return an empty response when no User DoesNotExist in the User Model, however I get the following error. How can i achieve this? Answer You are trying to serialize a queryset, so you need to add many=True to the serialization.
You must set settings.ALLOWED_HOSTS if DEBUG is False. Django-r-f
I want to prepare the server for deployment and this is the setup. settings.py is split into dev.py and prod.py in settings.py at the bottom ALLOWED_HOSTS = [] I also have tried ALLOWED_HOSTS = [‘*’] dev.py wsgy.py When I import settings in the manage.py shell, the settings.DEBUG = False .. why? How to make wsgi.py read the correct file? Answer
How to get the most liked users in django rest-api
So I have a social media app, where users can like the posts of other users. Now I want to fetch the top 20 users who have received the most number of likes. I am pretty much confused how to query my Likes Model My LIKES MODEL SIMPLIFIED POST MODEL SIMPLIFIED USER MODEL ** My View ** Answer First I
Best practise for creating database objects from .csv file
My situation is: having .csv file with some columns e.g. name, surname, age, activity, and others I want to create objects in my relational database with row from this file being single object. Column names correspond to model fields and with others being multiple columns defined by user who creates .csv (those others land in model’s filed comment separated with