mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-02 19:40:13 +03:00
Fix docs
This commit is contained in:
parent
4d01b85431
commit
98c101b2c1
|
@ -156,13 +156,14 @@ The following methods are provided by the mixin classes, and provide easy overri
|
|||
These hooks are particularly useful for setting attributes that are implicit in the request, but are not part of the request data. For instance, you might set an attribute on the object based on the request user, or based on a URL keyword argument.
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(user=self.request.user)
|
||||
return serializer.save(user=self.request.user)
|
||||
|
||||
These override points are also particularly useful for adding behavior that occurs before or after saving an object, such as emailing a confirmation, or logging the update.
|
||||
|
||||
def perform_update(self, serializer):
|
||||
instance = serializer.save()
|
||||
send_email_confirmation(user=self.request.user, modified=instance)
|
||||
return instance
|
||||
|
||||
You can also use these hooks to provide additional validation, by raising a `ValidationError()`. This can be useful if you need some validation logic to apply at the point of database save. For example:
|
||||
|
||||
|
@ -170,7 +171,7 @@ You can also use these hooks to provide additional validation, by raising a `Val
|
|||
queryset = SignupRequest.objects.filter(user=self.request.user)
|
||||
if queryset.exists():
|
||||
raise ValidationError('You have already signed up')
|
||||
serializer.save(user=self.request.user)
|
||||
return serializer.save(user=self.request.user)
|
||||
|
||||
**Note**: These methods replace the old-style version 2.x `pre_save`, `post_save`, `pre_delete` and `post_delete` methods, which are no longer available.
|
||||
|
||||
|
|
|
@ -99,7 +99,7 @@ The way we deal with that is by overriding a `.perform_create()` method on our s
|
|||
On the `SnippetList` view class, add the following method:
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(owner=self.request.user)
|
||||
return serializer.save(owner=self.request.user)
|
||||
|
||||
The `create()` method of our serializer will now be passed an additional `'owner'` field, along with the validated data from the request.
|
||||
|
||||
|
|
|
@ -46,7 +46,7 @@ Next we're going to replace the `SnippetList`, `SnippetDetail` and `SnippetHighl
|
|||
return Response(snippet.highlighted)
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(owner=self.request.user)
|
||||
return serializer.save(owner=self.request.user)
|
||||
|
||||
This time we've used the `ModelViewSet` class in order to get the complete set of default read and write operations.
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user