Skip to content

Inheriting multiple Serializers gives unexpected order of overloading of fields #5798

Closed
@svilendobrev

Description

@svilendobrev

Checklist

  • [ x] I have verified that that issue exists against the master branch of Django REST framework.
  • [ x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [ x] This is not a usage question. (Those should be directed to the discussion group instead.)
  • [ x] This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • [ x] I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions