question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug when using custom serializers

See original GitHub issue

Running 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:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jpdlorcommented, Sep 21, 2018

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…

0reactions
nils-wisiolcommented, Sep 27, 2018

We fixed it for us by having CurrentUserSerializer inherit from djoser’s UserSerializer 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found