Description
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?