mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-18 20:22:22 +03:00
added nullIfNotAuthed arg to CurrentUserDefault
to prevent error when using a optional user field and the user isnt authenticated
This commit is contained in:
parent
0abb84fa39
commit
7b2e8a12ff
|
@ -262,8 +262,12 @@ class CreateOnlyDefault:
|
|||
|
||||
class CurrentUserDefault:
|
||||
requires_context = True
|
||||
def __init__(self, nullIfNotAuthed = False):
|
||||
self.nullIfNotAuthed = nullIfNotAuthed
|
||||
|
||||
def __call__(self, serializer_field):
|
||||
if self.nullIfNotAuthed and not serializer_field.context['request'].user.is_authenticated:
|
||||
return None
|
||||
return serializer_field.context['request'].user
|
||||
|
||||
def __repr__(self):
|
||||
|
|
Loading…
Reference in New Issue
Block a user