Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for overriding fields in serializer mixins. #3699

Closed
wants to merge 0 commits into from

Conversation

craigds
Copy link
Contributor

@craigds craigds commented Dec 4, 2015

This is an updated version of my old PR (#1053) applied to current master.

The implementation is a bit simpler now that OrderedDict is a thing.

@tomchristie
Copy link
Member

Sorry - finding it a bit hard from the tests cases to see what the behavioral change is here.
Could you provide a simple example?

@craigds
Copy link
Contributor Author

craigds commented Dec 5, 2015

Sure

from rest_framework import serializers

class A(serializers.Serializer):
    myfield = serializers.IntegerField()

class B(serializers.Serializer):
    myfield = serializers.CharField()


class Sub(A, B):
    pass

# Original behaviour (wrong, confusing)
>>> Sub._declared_fields['myfield']
CharField()

# New behaviour (correct)
>>> Sub._declared_fields['myfield']
IntegerField()

@craigds
Copy link
Contributor Author

craigds commented Dec 5, 2015

Sorry, the title of the PR was slightly incorrect, probably why you're a bit confused. A case that actually works fine:

from rest_framework import serializers

class A(serializers.Serializer):
    myfield = serializers.IntegerField()

class Sub(A):
    myfield = serializers.FloatField()


>>> Sub._declared_fields['myfield']
FloatField()

I think that was also broken when I submitted my original PR years ago, hence the slightly confused title of this PR. It seems to have been fixed since then.

The diamond inheritance case is still broken, hence this PR.

@craigds craigds changed the title Add support for overriding fields in serializer subclasses. Add support for overriding fields in serializer mixins. Dec 5, 2015
@craigds
Copy link
Contributor Author

craigds commented Dec 5, 2015

Fixed title to match the actual fix

@craigds
Copy link
Contributor Author

craigds commented Jul 15, 2021

not sure why I closed this in 2016, but I have been maintaining a patch in our fork since then ...

Now upgrading to a modern DRF, I see it's fixed as of b8c369c (3.11+; #6980) 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants