Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ResourceRelatedField: resource_name doesn't work for nested serializers #303

Closed
@czosel

Description

I found another issue related to support for resource_name. While it works fine for to_representation when my resource has many related resources, i.e.

"relationships": {
 "portal-images": {
 "meta": {
 "count": 2
 },
 "data": [
 {
 "type": "ownerportal-image",
 (...)

(here, i could return ownerportal-image instead of portal-image successfully)
it doesn't work when the relationship is the other way around, i.e. my resource is related to only one other resource:

"relationships": {
 "image": {
 "data": {
 "type": "image",

(here, i would have expected ownerimage instead of image)

After debugging a bit, i found that the problem is related to (relations.py, 140)

resource_type = None
root = getattr(self.parent, 'parent', self.parent)
field_name = self.field_name if self.field_name else self.parent.field_name
if getattr(root, 'included_serializers', None) is not None:

In the successful case, self.parent refers to a ManyRelatedField, and self.parent.parent to my serializer.
In the unsuccessful case, self.parent refers to my serializer, while self.parent.parent is Null.

For me, changing the initialization of root to

root = getattr(self.parent, 'parent', self.parent) or self.parent

fixes the problem.

Since i'm a bit unsure if my fix is the right way to go, i thought i'd post this as an issue first to get your feedback. Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    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

        AltStyle によって変換されたページ (->オリジナル) /