Skip to content

Commit 75beb6a

Browse files
committed
Note on ValidationError in perform_create. Closes #2791.
1 parent 402fb39 commit 75beb6a

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

docs/api-guide/generic-views.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ These override points are also particularly useful for adding behavior that occu
188188
instance = serializer.save()
189189
send_email_confirmation(user=self.request.user, modified=instance)
190190

191+
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:
192+
193+
def perform_create(self, serializer):
194+
queryset = SignupRequest.objects.filter(user=self.request.user)
195+
if queryset.exists():
196+
raise ValidationError('You have already signed up')
197+
serializer.save(user=self.request.user)
198+
191199
**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.
192200

193201
**Other methods**:

0 commit comments

Comments
 (0)