How can I remove the upper array list from the list of dictionaries?

I’ve a list of dictionaries and want to get rid of the external list array. Take into consideration the array below, My serializer looks like, and the view definition is, Answer You can use the index of list to refer to the inner dictionary and omit the outer bracket. The below is the output of a[0]:

Django Rest Framework With Only Function Response

I just want to make a simple API with DRF. The API going to get a “keyword” from URL, and that “keyword” will go to a function. And function response is going to be API response. Simple Example: http://127.0.0.1:8000/api/langdetect/helloworld/ RESULT: It’s all easy. How can I make this? I read the doc but every example is making with Models, serializers etc. I don’t have any models. I have only a function and the response data. Thanks. Answer You’ll want a bare APIView if you want to use DRF’s permissions and content type negotiation system yet still keep things simple:

JSONField in Django continues to remain empty even after POST request

I have a model with a JSONField in Django. If I issue POST through the browser using Django Rest UI, the data gets entered into the model with no issues. However,when I use Python’s requests.post in my application, everything except the JSONField data stores in the model. Here is my model Here is my Serializer Here is my view Here is my request URL What I see after using requests.post Answer try data in this format . and remove jsonvalue

Capturing values from url in post request in Django

I want to capture url values from a url into my views in a post request. My urls.py looks like below from django.urls import path from . import views as projects_views and I want to capture the value project_id in my ListCreateAPIView. Get call works fine. My views.py looks like below How can this be done? Answer Use self.kwargs[“project_id”] to get the project_id in perform_create() method,

cant fix error: create() takes 1 positional argument but 2 were given and cant seem to fix it

I have a django rest framework view set. I want to create an object based on data that is passed in with the post request. I have it setup right now to create new object with dummy data before it starts to take information from the request. I am getting the following error: Here is my viewset method: Answer Pass keyword arguments to create method like **data instead of data. Change return Preference.objects.create(data) to return Preference.objects.create(**data)

How do I access a model’s nested one-to-many relations?

I have three models as shown below: A restaurant has many menu categories, which has many menu items. Is this the right way to lay out these relationships? What’s the most efficient way to get ALL the menu items for a given restaurant? I’m hoping there’s a more effective way than to loop across all menu categories for a given restaurant. Answer You can obtain all MenuItems for a given restaurant with: where my_restaurant is the Restaurant object for which you want to retrieve the MenuItems. Django will construct a query for this, and the database will perform JOINs, such

Django Rest Framework – AssertionError Fix your URL conf, or set the `.lookup_field` attribute on the view correctly

I’m trying to return as single object (not a queryset) that is specific to a user without them having to specify an identifier/pk within the requested URL. Each user has an organisation FK. i.e. http://website/organisation and not http://website/organisation/1 I’m receiving the following error, since it’s expecting this identifier: AssertionError: Expected view OrganisationDetail to be called with a URL keyword argument named “user__organisation_id”. Fix your URL conf, or set the.lookup_fieldattribute on the view correctly. How/What do I need to specify when using the RetrieveModelMixin/GenericAPIViewso that it returns a singular object linked by a FK? My view class: Related url: url(r’^api/v1/organisation/$’, OrganisationDetail.as_view()),

Get current user in Model Serializer

Is it possible to get the current user in a model serializer? I’d like to do so without having to branch away from generics, as it’s an otherwise simple task that must be done. My model: My serializer: And my view: How can I get the model returned, with an additional field user such that my response looks like this: Answer I found the answer looking through the DRF source code. The key is the fact that methods defined inside a ModelSerializer have access to their own context, which always includes the request (which contains a user when one is