Skip to content

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

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

Closed
czosel opened this issue Nov 30, 2016 · 1 comment
Closed

Comments

@czosel
Copy link
Contributor

czosel commented Nov 30, 2016

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!

@mblayman
Copy link
Collaborator

Fixed by #306.

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

No branches or pull requests

2 participants