Skip to content

Delete Relationships #169

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
Reizoukodesu opened this issue Dec 11, 2015 · 3 comments
Closed

Delete Relationships #169

Reizoukodesu opened this issue Dec 11, 2015 · 3 comments

Comments

@Reizoukodesu
Copy link

Hey thanks for the nice work. But I've got an issue when I try to delete an relationship over the "relationships" JSON attribute. For example the following request won't delete a relationship:

{
  "data": {
     "id": 1,
     "type": "model",
     "attributes": {
       "name": "test-model",
       "number": 1
     },
     "relationships": {
       "other-model": {"data": null}
     }
  }
}

However when I move the relationship to the attributes field, it is being deleted successfully:

{
  "data": {
     "id": 1,
     "type": "model",
     "attributes": {
       "name": "test-model",
       "number": 1,
       "other-model": null
     }
  }
}

I've debugged the code and made some tests. The file rest_framework_json_api/parsers.py:41 does not handle this specific case when the data attribute is None. But as stated in the JSON API specification, the relation should be deleted when this case is given, as here explained.

Changing the mentioned code to the following example seems to fix this issue:

    @staticmethod
    def parse_relationships(data):
        relationships = (utils.format_keys(data.get('relationships'), 'underscore')
                         if data.get('relationships') else dict())

        # Parse the relationships
        parsed_relationships = dict()
        for field_name, field_data in relationships.items():
            field_data = field_data.get('data')
            if isinstance(field_data, list):
                parsed_relationships[field_name] = list(relation for relation in field_data)
            else:
                parsed_relationships[field_name] = field_data
        return parsed_relationships

Is this method of deleting relationships going to be supported by this framework or should an alternative method being used to delete relationships?

@jsenecal
Copy link
Member

Hey there - thanks for reporting this :)

At first I was skeptical at your explanation as you seemed to have mixed up few concepts as dealing with relationships at the resource level is different from dealing with relationships from a relationship link. This link is about relationship links (here implemented by RelationshipViews)

However, since you seem to have figured a way to handle your specific issue, I would suggest you fork the project and submit a PR for reviewal. Adding some failing tests prior to this fix would also help.

Thanks :)

@abdulhaq-e
Copy link
Contributor

But according to the link from the specs, updating a relationship should be done by a PATCH/POST/DELETE to the relationship link. For example:

PATCH /articles/1/relationships/author HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": null
}.

From what I understood, you are trying to update a relationship directly (via the top level data object) rather than using the relationship link as the specs say.

Forget about what I said, the specs indeed say:

Although relationships can be modified along with resources (as described above), JSON API also supports updating of relationships independently at URLs from relationship links.

arthur-s added a commit to arthur-s/django-rest-framework-json-api that referenced this issue Dec 25, 2015
@Reizoukodesu
Copy link
Author

The issue is being closed because the changes are merged

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

3 participants