Skip to content

Relationship deletion #171

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

Merged

Conversation

Reizoukodesu
Copy link

Allow the deletion of relationships using the relationships values of a request, eg:

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

See JSON API specification

References #169

Allow the deletion of relationships using the `relationships` values of a request, eg:

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

See http://jsonapi.org/format/#crud-updating-to-one-relationships
@jerel
Copy link
Member

jerel commented Dec 14, 2015

This looks good, thanks! Would you mind adding a test to this file to demonstrate the fix? https://github.com/django-json-api/django-rest-framework-json-api/blob/develop/example/tests/test_views.py#L140-L146 You can use the highlighted test as an example.

To delete a relationship, the corresponding value
is being overriden with None. Eg:
{
    'data': {
        'type': 'posts',
        'relationships': {
            'blog': {
                'data': None
            }
        }
    }
}
@Reizoukodesu
Copy link
Author

Added a unit test for relationship deletion using the relationships attribute. I've used the blog field of an Entry instance. But as this field cannot be Null, the exception message is being tested and compared to "This field may not be null."

@jsenecal
Copy link
Member

This does not test the added functionality unfortunately... Any idea on what to add to the example code to have a nullable to-one relationship?

@abdulhaq-e
Copy link
Contributor

I think the model can be redefined inside the test function.

@Reizoukodesu
Copy link
Author

After a closer look at the Model classes I've found a working use case, so I changed the deletion test to use the author relation field from the second comment.

jerel added a commit that referenced this pull request Dec 29, 2015
@jerel jerel merged commit bf7bd7b into django-json-api:develop Dec 29, 2015
@jerel
Copy link
Member

jerel commented Dec 29, 2015

Thank you!

@jsenecal
Copy link
Member

Yes, thank you :) can we close #169?

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

Successfully merging this pull request may close these issues.

4 participants