-
Notifications
You must be signed in to change notification settings - Fork 300
-
Hi,
I'm currently working on a project using django-rest-framework-json-api, and I have a use case where I need to include an extra attribute within the data objects in a relationship. Here’s an example of what I'm trying to achieve:
"relationships": {
"responsible_supplier": {
"data": [{
"type": "supplier",
"id": "c00f7b66-6768-4b42-b72e-bf2b018210c5",
"level": 1
}, {
"type": "supplier",
"id": "8999d741-29ed-4d07-b228-7aa79b43d9a7",
"level": 2
}]
}
}
I'm looking to add a level field for each supplier within the relationships section. However, from what I understand, JSON
only supports type and id fields directly in the data objects.
Here's how I am currently resolving this with my serializers:
class ResponsibleSupplierSerializer(ModelSerializer):
id = PrimaryKeyRelatedField(queryset=Supplier.objects.all())
level = IntegerField(min_value=1, write_only=True)
class Meta:
model = ResponsibleSupplier
fields = ['id', 'level']
class MaintenanceSerializer(ModelSerializer):
responsible_suppliers = ResourceRelatedField(many=True, queryset=Supplier.objects.all())
I was wondering if anyone else has encountered a similar requirement and how you approached it. Are there any workarounds or best practices to include additional attributes like level within relationships, while still adhering to the JSON
specification?
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 1 reply
-
Thinking about it I guess you have two options.
- you use
metaon the relationship. this is not supported yet in DJA see Allow meta attributes in relationship fields #406 so you would need a custom renderer overridingextract_relationshipsfor your use case - you add the level to the relationship serializer and include it in the
included_serializers.
Beta Was this translation helpful? Give feedback.
All reactions
-
I appreciate the suggestion, but my question isn't about adding additional attributes in the data rendering phase (GET requests). I'm specifically looking to handle extra fields like level during POST or PATCH operations.
For example, when submitting data for creation or updates, I want to include additional attributes in the relationships object
Beta Was this translation helpful? Give feedback.
All reactions
-
My first suggestion also works on writing operations, in fact not just on the relationships key itself but on each relationship field you can have a meta object which might be better for what you are looking for.
You can see this in the JSON:API spec where it shows that relationships is a list of resource identifier objects and there it states that a meta key is allowed.
In your case this could look like this:
"relationships": { "responsible_supplier": { "data": [{ "type": "supplier", "id": "c00f7b66-6768-4b42-b72e-bf2b018210c5", "meta": {"level": 1} }, { "type": "supplier", "id": "8999d741-29ed-4d07-b228-7aa79b43d9a7", "meta": {"level": 2} }] } }
I haven't used this before but to parse something like this you would need to implement your custom ResourceRelatedField and overwrite to_internal_value which will handle the meta-object as you need it. Hope this helps.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1