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

StringRelatedField() not working#1234

Answeredbysliverc
Jer-Pha asked this question inQ&A
Discussion options

I'm putting this here because it's probably user error, not an issue with the repo. I'm new to DRF, so this might be something really obvious.

CallingStringRelatedField removes these fields from "relationships". If I disabledjangorestframework-jsonapi, the standard API shows the string values correctly.

models.py

...classModel_D(models.Model):model_a=models.ManyToManyField(Model_A)model_b=models.ForeignKey(Model_B,on_delete=models.CASCADE)model_c=models.ForeignKey(Model_C,on_delete=models.CASCADE)...

serializers.py

fromrest_framework_json_apiimportserializers...classModel_DSerializer(serializers.ModelSerializer):    ...model_a=serializers.StringRelatedField(many=True)model_b=serializers.StringRelatedField()model_c=serializers.StringRelatedField()classMeta:model=Episodefields= ["model_a","model_b","model_c",        ]

I've tried changingto_representation() inModelDSerializer, instead of usingStringRelatedField():

defto_representation(self,obj):return {            ..."model_a":str(obj.model_a),"model_b":str(obj.model_b),"model_c":str(obj.model_c),            ...        }

For the ForeignKey fields, this replaced the data dict (type, id) with just the string value. It didn't do anything for the ManyToManyField.

I really like this package but I might not be able to use it (I don't want the ManyToMany primary keys visible in the API).

Note: the models are in four different apps, if that affects anything. They all havedef __str__(self): set up correctly.

You must be logged in to vote

Thanks for bringing this up. It seems that in DJA we have not covered the use case ofStringRelatedField so that is the reason it is not working. Feel free to create an issue for this.

As a workaround though, what you can do is to create your ownResourceRelatedField and overwriteget_resource_id to return a string. You can use this field instead ofStringRelatedField.

This could look like the following:

classStringResourceRelatedField(ResourceRelatedField):defget_resource_id(self,resource):returnstr(resource)

Using such a field would have the same effect asStringRelatedField but would of course only work with the DJA renderer.

Replies: 1 comment

Comment options

Thanks for bringing this up. It seems that in DJA we have not covered the use case ofStringRelatedField so that is the reason it is not working. Feel free to create an issue for this.

As a workaround though, what you can do is to create your ownResourceRelatedField and overwriteget_resource_id to return a string. You can use this field instead ofStringRelatedField.

This could look like the following:

classStringResourceRelatedField(ResourceRelatedField):defget_resource_id(self,resource):returnstr(resource)

Using such a field would have the same effect asStringRelatedField but would of course only work with the DJA renderer.

You must be logged in to vote
0 replies
Answer selected byJer-Pha
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
@Jer-Pha@sliverc

[8]ページ先頭

©2009-2025 Movatter.jp