Bug when using custom serializers
See original GitHub issueRunning Djoser 1.3.0 on Django 2.1.1
I can successfully create and use one custom serializer like so:
# users/serializers.py
class CustomUserCreateSerializer(UserCreateSerializer):
class Meta:
fields = tuple(User.REQUIRED_FIELDS) + (
User.USERNAME_FIELD, User._meta.pk.name, 'password',
'is_manager',
)
# settings.py
DJOSER = {
'SERIALIZERS': {
'user_create': 'users.serializers.CustomUserCreateSerializer',
},
}
But as soon as I create a second serializer I get an error:
# users/serializers.py
class CustomUserCreateSerializer(UserCreateSerializer):
class Meta:
fields = tuple(User.REQUIRED_FIELDS) + (
User.USERNAME_FIELD, User._meta.pk.name, 'password',
'is_manager',
)
class CustomUserSerializer(UserSerializer):
class Meta:
model = User
fields = tuple(User.REQUIRED_FIELDS) + (
User._meta.pk.name,
User.USERNAME_FIELD,
'is_manager',
)
read_only_fields = (User.USERNAME_FIELD,)
# settings.py
DJOSER = {
'SERIALIZERS': {
'user_create': 'users.serializers.CustomUserCreateSerializer',
'user': 'users.serializers.CustomUserSerializer',
},
}
I receive this error: ImportError: Module "users.serializers" does not define a "UserSerializer" attribute/class
.
The traceback leads me back to class CurrentUserSerializer(settings.SERIALIZERS.user):
in djoser/serializers.py. If I remove that CurrentUserSerializer
class, or if I change its inherited class to serializers.ModelSerializer
then everything runs as expected.
My guess is that this is a sort of circular dependency issue with the new CurrentUserSerializer, but I’m not quite sure how to solve it in my code (or if that’s possible).
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Custom error messages in Django Rest Framework serializer
I have a model with some fields that are required. Let's say one of them is a TextField which can't be blank ....
Read more >[Tinkerpop 3] Problem with custom class Serialization
I'm currently working on my own implementation of Tinkerpop3 Graph. I would like to be able to serialize my graphs to be able...
Read more >How to write custom converters for JSON serialization - .NET
Learn how to create custom converters for the JSON serialization classes that are provided in the System.Text.Json namespace.
Read more >Serializers - Django REST framework - Tom Christie
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered...
Read more >Custom serialization - Serde
Serde's derive macro through #[derive(Serialize, Deserialize)] provides reasonable ... serializer: S) -> Result<S::Ok, S::Error> where S: Serializer; ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Same problem. But found a workaround by putting the CustomUserSerializer class in another file than the CustomUserCreateSerializer class and thus having 2 different imports from the SERIALIZERS setting. However it is ugly…
The djoser/serializers.py code
class CurrentUserSerializer(settings.SERIALIZERS.user):
is definitively the culprit, yes, (it creates a circular relationship). As a matter of fact I don’t understand why this CurrentUserSerializer class is here since it just for a deprecation warning which itself is triggered by the totally legit route /auth/users/me…We fixed it for us by having
CurrentUserSerializer
inherit from djoser’sUserSerializer
instead of the settings string. (This was also the case in Djoser before the deprication warning was introduced.)Thanks for handling this so quickly, @joshua-s