added nullIfNotAuthed arg to CurrentUserDefault

to prevent error when using a optional user field and the user isnt authenticated
This commit is contained in:
Michele Awada 2023-12-01 15:14:51 +03:00 committed by GitHub
parent 0abb84fa39
commit 7b2e8a12ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -262,8 +262,12 @@ class CreateOnlyDefault:
class CurrentUserDefault: class CurrentUserDefault:
requires_context = True requires_context = True
def __init__(self, nullIfNotAuthed = False):
self.nullIfNotAuthed = nullIfNotAuthed
def __call__(self, serializer_field): 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 return serializer_field.context['request'].user
def __repr__(self): def __repr__(self):