-
Notifications
You must be signed in to change notification settings - Fork 301
Set custom "type" for ResourceRelatedField #193
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
Comments
It should now possible to specify the resource name on the model class, thanks to #152 . for example: class Comment(models.Model):
class JSONAPIMeta:
resource_name = 'comments'
# the rest of the model definition... |
leifurhauks' example should solve this. I'll close and if it doesn't fix then let us know. |
Thanks! Please, describe this feature in the documentation. class MySerializer(serializers.ModelSerializer):
parent = relations.ResourceRelatedField(source='comment_parent', model=Comment, read_only=True) But it does not work. It will work properly if override the following lines in renderers.py: @staticmethod
def extract_relationships(fields, resource, resource_instance):
# ....
if isinstance(field, ResourceRelatedField):
# special case for ResourceRelatedField
if hasattr(field, 'model'):
res_data = resource.get(field_name)
relation_data = {
'data': OrderedDict([('type', relation_type), ('id', res_data['id'])]) if res_data else None
}
else:
relation_data = {
'data': resource.get(field_name)
}
# .... I'm not sure that it is right approach, so will use leifurhauks' solution, but it very seems like a bug -) |
If I remember correctly, the ResourceRelatedField used to use the model kwarg to determine the type name when read-only, but I think this behaviour may have been changed since then to support heterogeneous to-many relationships (e.g. polymorphic relationships with objects of more than one type). |
Hi, all!
I have the following Django model:
I want to be able to obtain a list of comments with "parent" in relationships, for example:
My serializer:
In my ViewSet I have defined resource_name = 'comments', but it is doesn't work for "parent" in relationships. At the present time, "type" of "parent" is "Comment". How can I fix this issue?
The text was updated successfully, but these errors were encountered: