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

Using?include withHyperlinkedRelatedField#1154

Answeredbysliverc
Nekidev asked this question inQ&A
Discussion options

I want to useHyperlinkedRelatedField for my serializer relationships since there are lots of relationships in my many-to-many rel and my server resources are limited, but allow users to use?included=rel if they need it. Currently, if I use?include= with aHyperlinkedRelatedField, I get:

Traceback (mostrecentcalllast):File"...\api\.venv\lib\site-packages\asgiref\sync.py",line534,inthread_handlerraiseexc_info[1]File"...\api\.venv\lib\site-packages\django\core\handlers\exception.py",line42,ininnerresponse=awaitget_response(request)File"...\api\.venv\lib\site-packages\django\core\handlers\base.py",line284,in_get_response_asyncresponse=awaitsync_to_async(File"...\api\.venv\lib\site-packages\asgiref\sync.py",line479,in__call__ret:_R=awaitloop.run_in_executor(File"...\api\.venv\lib\site-packages\asgiref\current_thread_executor.py",line40,inrunresult=self.fn(*self.args,**self.kwargs)File"...\api\.venv\lib\site-packages\asgiref\sync.py",line538,inthread_handlerreturnfunc(*args,**kwargs)File"...\api\.venv\lib\site-packages\django\template\response.py",line114,inrenderself.content=self.rendered_contentFile"...\api\.venv\lib\site-packages\rest_framework\response.py",line70,inrendered_contentret=renderer.render(self.data,accepted_media_type,context)File"...\api\.venv\lib\site-packages\rest_framework_json_api\renderers.py",line568,inrenderself.extract_included(File"...\api\.venv\lib\site-packages\rest_framework_json_api\renderers.py",line338,inextract_includedrelation_queryset=list(relation_instance)TypeError:'NoneType'objectisnotiterable

I suppose this is because the field is not intended to load the data and therefore raises an error when the data to include isNone. Is there any way to make the field have this behavior?

You must be logged in to vote

If you use hyperlinked related fields, if you have a resource in the included array, you do not know where it would be linked to. Therefore, as I see it, it is against the spec to allow this. Seehere where it talks about full-linkage which needs to be clear by the compound document. Anyhow, I am not so sure about the use case anyway.

My suggestion is to either have all relationships as hyperlinks or use included serializers, but not both together.

Replies: 2 comments 1 reply

Comment options

If you use hyperlinked related fields, if you have a resource in the included array, you do not know where it would be linked to. Therefore, as I see it, it is against the spec to allow this. Seehere where it talks about full-linkage which needs to be clear by the compound document. Anyhow, I am not so sure about the use case anyway.

My suggestion is to either have all relationships as hyperlinks or use included serializers, but not both together.

You must be logged in to vote
0 replies
Answer selected byNekidev
Comment options

This is what I meant:

// No ?included=images{"data":{        ...,"relationships":{"images":{"links":{"self":"URL","related":"URL",}}}}}// With ?included=images{"data":{        ...,"relationships":{"images":{"meta":{"count":1},"data":[{"type":"image","id":"500c5ecf-8d83-4333-8f0f-207941aaa552"}],"links":{"self":"URL","related":"URL",}}}},"included":[{"type":"image","id":"500c5ecf-8d83-4333-8f0f-207941aaa552","attributes":{                ...},            ...}]}

This way, the user can select what to load and prevent from sending a big body with things the user won't use. In my app I have big relationships (1k resources in many-to-many) and responses take too much time to get delivered.

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

OK I see. You can do this somehow with a custom ResourceRelatedField and custom JSONRenderer. However, I do not think that is the intended use case for compound documents, that when using include query parameter the JSON:API structure suddenly changes.

If you have issues with too large bodies, I rather recommend you use sparse field sets as perspec where you can define what fields you need returned. This is supported by DJA out of the box as long as you use rest_framework_json_api.serializers.ModelSerializer for your serializers.

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

[8]ページ先頭

©2009-2025 Movatter.jp