Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to include an additional attribute in relationships data objects?#1262

jaimeres started this conversation inGeneral
Discussion options

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.

You must be logged in to vote

Replies: 2 comments 1 reply

Comment options

Thinking about it I guess you have two options.

  1. you usemeta on the relationship. this is not supported yet in DJA seeAllow meta attributes in relationship fields #406 so you would need a custom renderer overridingextract_relationships for your use case
  2. you add the level to the relationship serializer and include it in theincluded_serializers.
You must be logged in to vote
0 replies
Comment options

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

You must be logged in to vote
1 reply
@sliverc
Comment options

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 ameta object which might be better for what you are looking for.

You can see this in the JSON:API spec where it shows thatrelationships is a list ofresource 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 customResourceRelatedField and overwriteto_internal_value which will handle themeta-object as you need it. Hope this helps.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
General
Labels
None yet
2 participants
@jaimeres@sliverc

[8]ページ先頭

©2009-2025 Movatter.jp