mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-03 12:00:12 +03:00
Merge 98c101b2c1
into 489d3415ab
This commit is contained in:
commit
ccb22f36b4
|
@ -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.
|
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):
|
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.
|
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):
|
def perform_update(self, serializer):
|
||||||
instance = serializer.save()
|
instance = serializer.save()
|
||||||
send_email_confirmation(user=self.request.user, modified=instance)
|
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:
|
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)
|
queryset = SignupRequest.objects.filter(user=self.request.user)
|
||||||
if queryset.exists():
|
if queryset.exists():
|
||||||
raise ValidationError('You have already signed up')
|
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.
|
**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:
|
On the `SnippetList` view class, add the following method:
|
||||||
|
|
||||||
def perform_create(self, serializer):
|
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.
|
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)
|
return Response(snippet.highlighted)
|
||||||
|
|
||||||
def perform_create(self, serializer):
|
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.
|
This time we've used the `ModelViewSet` class in order to get the complete set of default read and write operations.
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ class CreateModelMixin(object):
|
||||||
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
||||||
|
|
||||||
def perform_create(self, serializer):
|
def perform_create(self, serializer):
|
||||||
serializer.save()
|
return serializer.save()
|
||||||
|
|
||||||
def get_success_headers(self, data):
|
def get_success_headers(self, data):
|
||||||
try:
|
try:
|
||||||
|
@ -77,7 +77,7 @@ class UpdateModelMixin(object):
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
def perform_update(self, serializer):
|
def perform_update(self, serializer):
|
||||||
serializer.save()
|
return serializer.save()
|
||||||
|
|
||||||
def partial_update(self, request, *args, **kwargs):
|
def partial_update(self, request, *args, **kwargs):
|
||||||
kwargs['partial'] = True
|
kwargs['partial'] = True
|
||||||
|
|
Loading…
Reference in New Issue
Block a user