I have a Book model with a foreign key to user (the owner of the book):
class Book(models.Model):
owner = models.ForiegnKey(User)
...
I've created a ModelViewSet for Book which shows the books owned by the logged in user:
class BookViewSet(viewsets.ModelViewSet):
model = Book
serializer_class = BookSerializer
def get_queryset(self):
return Book.objects.filter(owner=self.request.user)
Now to create a new book, I want to save user field with request.user, not with data sent from the rest client (for more security). for example:
def create(self, request, *args, **kwargs):
request.DATA['user'] = request.user
... (some code to create new Book from request.DATA using serialize class)
but I got this error: This QueryDict instance is immutable. (means request.DATA is a immutable QueryDict and can't be changed)
Do you know any better way to add additional fields when creating an object with django rest framework?
Update: Since v3 you need to do this:
The principle remains the same.
You want to make the
owner
field of your book serializer read-only and then set the association with the user inpre_save()
.Something like:
See the tutorial section on "Associating Snippets with Users".
I hope that helps.
If you're using
ModelSerializer
it's as easy as implementing therestore_object()
method:restore_object()
is used to deserialize a dictionary of attributes into an object instance.ModelSerializer
implements this method and creates/updates the instance for the model you specified in theMeta
class. If the giveninstance
isNone
it means the object still has to be created. In this case you just set theuser
field with the desired value.More information: http://www.django-rest-framework.org/api-guide/serializers#declaring-serializers
For what it's worth this has changed in Django REST Framework 3. There is now a
perform_create()
that replaces the olderpre_save()
andpost_save()
hooks suggested in a previous answer. For example:Hope this saves someone some time.