Skip to content

Issue with relationships in POST/PATCH/PUT requests #122

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
martinmaillard opened this issue Oct 9, 2015 · 13 comments
Closed

Issue with relationships in POST/PATCH/PUT requests #122

martinmaillard opened this issue Oct 9, 2015 · 13 comments

Comments

@martinmaillard
Copy link
Contributor

If I do a POST request with data containing a relationship:

{
    "data": {
        "type": "comments",
        "attributes": {
            "body": "Hi"
        },
        "relationships": {
            "entry": {
                "data": {
                    "id": "12",
                    "type": "entries"
                }
            }
        }
    }
}

I get the following error:

{
    "errors": [{
        "status": "400",
        "source": {"pointer": "/data/attributes/entry"}, 
        "detail": "Incorrect type. Expected pk value, received OrderedDict."
    }]
}

I understand why this is happening, but I think this is a mistake.

@martinmaillard
Copy link
Contributor Author

This behaviour seems to have been introduced in d654c75

@jerel
Copy link
Member

jerel commented Oct 9, 2015

It looks like this is probably a documentation bug on our part: you need to use ResourceRelatedField (from rest_framework_json_api.relations) for your relationship fields so it knows how to parse the incoming compound document

@jsenecal
Copy link
Member

jsenecal commented Oct 9, 2015

@martinmaillard, @jerel is right - i introduced ResourceRelatedField to be able to parse things properly: 23d0af6

See issue #111

@jsenecal
Copy link
Member

jsenecal commented Oct 9, 2015

We could also override the serializer_related_field in both HyperlinkedModelSerializer and ModelSerializer so that we use the ResourceRelatedField by default, but in the case of HyperlinkedModelSerializer we would also need to override a few of the methods provided by DRF to supply the proper links.

@martinmaillard
Copy link
Contributor Author

Ok thank you, I didn't know that :)

Now it's working, but I still have an issue with the type validation... ResourceRelatedField is way too rigid when it checks the valid type. It gets the expected type from the queryset, which means that:

  • it's not possible to customize the type of a relation (what is allowed by the resource_name on the serializer)
  • it's not possible to allow multiple types to be valid for one relation (useful with polymorphic relationships)

@jsenecal
Copy link
Member

jsenecal commented Oct 9, 2015

@martinmaillard I'm open to suggestions and PRs if you find a better way to validate the type as I am going to be busy for some time.

The expected type verification is done here: https://github.com/django-json-api/django-rest-framework-json-api/blob/develop/rest_framework_json_api/relations.py#L110 - have fun ;)

@jsenecal
Copy link
Member

jsenecal commented Oct 9, 2015

I also think we should create a new issue for the behavior you are describing as it is out of the topic of this one.

This issue is a duplicate of #111, I will now close it.

@jsenecal jsenecal closed this as completed Oct 9, 2015
@martinmaillard
Copy link
Contributor Author

I will think about it, but it's a complicated issue. These types are all over the place and we need to centralize the way we generate/validate them.

Anyway thank you for your quick answers !

@jsenecal
Copy link
Member

jsenecal commented Oct 9, 2015

No worries - also this issue made us realize we really need to document those features - I'll create a new one for that.

@riklaunim
Copy link
Contributor

How the ResourceRelatedField should be used? I still get that the related object is "places" instead of "Place" when creating an object with relation to Place.

@riklaunim
Copy link
Contributor

Any update on this? I would say expected type should not be the model name - https://github.com/django-json-api/django-rest-framework-json-api/blob/develop/rest_framework_json_api/relations.py#L119 as everywhere else it's plural lowercase version of it.

@jerel
Copy link
Member

jerel commented Jan 12, 2016

@riklaunim here is an example of the ResourceRelatedField in use: https://github.com/django-json-api/django-rest-framework-json-api/blob/develop/example/serializers.py#L40-L41 If you could create a test that shows the unexpected behavior and open a PR we could discuss it better.

@riklaunim
Copy link
Contributor

ok, I missed two things ;)

JSON_API_FORMAT_RELATION_KEYS = 'dasherize'
JSON_API_PLURALIZE_RELATION_TYPE = True

And it works.

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

No branches or pull requests

4 participants