Swagger (that is, drf-yasg2) tries to invoke every API view when generating a schema to render. When doing this, obviously, it cannot be "logged in" as a real user. Instead it uses Django's built-in AnonymousUser. This is a very stripped-down version of a User and it certainly doesn't have any of the custom fields of the User model defined in django-astrosat-users.
Some of our views rely on those extra fields, though, to do clever things. The recommended way to get around this issue is to protect the view by checking if it is being called in the context of swagger's schema generation and then just returning immediately if so [as per https://github.com/axnsan12/drf-yasg/issues/333#issuecomment-474883875].
However, this issue also crops up when using CurrentUserDefault w/ SlugRelatedFields on the serializer (since AnonymousUser is very unlikely to have the slug_field referred to in the serializer definition). Therefore, I should add a similar check to my custom SwaggerCurrentUserDefault class.
Additional context
This is not required, it doesn't affect functionality. But it does spam the server logs w/ crap that makes it hard for me to isolate real errors.
Describe the feature
Swagger (that is,
drf-yasg2
) tries to invoke every API view when generating a schema to render. When doing this, obviously, it cannot be "logged in" as a real user. Instead it uses Django's built-inAnonymousUser
. This is a very stripped-down version of aUser
and it certainly doesn't have any of the custom fields of theUser
model defined in django-astrosat-users.Some of our views rely on those extra fields, though, to do clever things. The recommended way to get around this issue is to protect the view by checking if it is being called in the context of swagger's schema generation and then just returning immediately if so [as per https://github.com/axnsan12/drf-yasg/issues/333#issuecomment-474883875].
However, this issue also crops up when using
CurrentUserDefault
w/SlugRelatedFields
on the serializer (sinceAnonymousUser
is very unlikely to have theslug_field
referred to in the serializer definition). Therefore, I should add a similar check to my customSwaggerCurrentUserDefault
class.Additional context
This is not required, it doesn't affect functionality. But it does spam the server logs w/ crap that makes it hard for me to isolate real errors.