Reputation: 151
I am learning Django and Django Rest Framework to create an API and cannot wrap my head around why when i run this command:
http --json POST http://127.0.0.1:8000/api/v1/stocks/ book_code='Abook' 'Authorization: Token 123243434354353'
i get an error which says:
HTTP/1.0 400 Bad Request
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Date: Thu, 25 May 2017 19:16:37 GMT
Server: WSGIServer/0.2 CPython/3.6.0
Vary: Accept
X-Frame-Options: SAMEORIGIN {
"user": [
"This field is required."
]}
The 'user' field is required but I am expecting it to populate with the user based on the token I have provided i.e the authenticated user.
This is the bookstock/models.py
:
from django.db import models
from django.contrib.auth.models import User
class Stock(models.Model):
'''
Model representing the stock info.
'''
user = models.ForeignKey(User)
book_code = models.CharField(max_length=14, null=True, blank=True)
def __str__(self):
return self.book_code
This is the api/serializer.py
:
from bookstock.models import Stock
from rest_framework import serializers
class StockSerializer(serializers.ModelSerializer):
class Meta:
model = Stock
fields = ('id', 'user', 'book_code')
This is the api/views.py
:
from rest_framework import generics
from bookstock.models import Stock
from api.serializers import StockSerializer
from rest_framework.permissions import IsAuthenticated
class StockList(generics.ListCreateAPIView):
serializer_class = StockSerializer
permission_classes = (IsAuthenticated,)
def get_queryset(self):
user = self.request.user
return Stock.objects.filter(user=user)
def perform_create(self, serializer):
serializer.save(user=self.request.user, )
def perform_update(self, serializer):
serializer.save(user=self.request.user)
This is the api/urls.py
:
from django.conf.urls import url, include
from api import views
from rest_framework.authtoken.views import obtain_auth_token
urlpatterns = [
url(r'^v1/stocks/$', views.StockList.as_view()),
url(r'^v1/api-token-auth/', obtain_auth_token),
url(r'v1/api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
What am I missing? Based on answers from SO, the perform_create
and perform_update
override should be populating the user right?
Upvotes: 2
Views: 1478
Reputation: 2945
The perform_create
method is called by your View's create
method after serializer validation. At this stage, DRF will have found and flagged the missing user field: https://github.com/encode/django-rest-framework/blob/master/rest_framework/mixins.py#L14
There are multiple ways around that, ie setting the serializer field to read_only=True
or overriding the validate_user
method on the serializer.
The most elegant way I think is to use DRF's currentuserdefault
validator: http://www.django-rest-framework.org/api-guide/validators/#currentuserdefault
user = serializers.HiddenField(default=serializers.CurrentUserDefault())
(replace HiddenField
with CharField
or whatever you prefer)
Upvotes: 2