-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Inheriting multiple Serializers gives unexpected order of overloading of fields #5798
Comments
Hi. From the description, and from experience of working with inheritance and serialisers, the behaviour you describe sounds expected. I'm going to close this as such. If you can expand your "Steps to Reproduce" to include an actual (and minimal) code reproduce then I'm happy to re-open. (As it stands the issue is not actionable.) Thanks. |
Hi @carltongibson, I've run into this before. Here's a minimal example:
Under normal Python MRO, you'd expect the |
It would have the intuitive behavior if
|
Processing the mro in reverse is correct, since we want classes higher up in the mro to be overridden by those lower in the mro. The issue is the subsequent line, that prepends fields, effectively reversing order again (as pointed out by @svilendobrev). Reopening as this looks valid, and the above is enough of a test case to work with. |
Checklist
master
branch of Django REST framework.Steps to reproduce
inheriting a serializer from several other serializers. Some of them have fields with same name, and that name is not present localy in inheriting one
Expected behavior
from those same-name fields, the first as in the order of inheritance should win
Actual behavior
last one wins.
This is because in SerializerMetaclass._get_declared_fields():
a) bases is walked in reverse
b) fields are prepended (i.e. reversing order once more)
c) repeating field-names are added just as any other; check is only against local-attrs
d) result list goes into ordereddict... hence last wins.
Easiest remedy is to reverse bases once more on entrance to this func...
or to avoid breaking unknown amounts of code that relies on current behavior, keep it as is, and put big warning somewhere as documentation.
have fun
The text was updated successfully, but these errors were encountered: