- Notifications
You must be signed in to change notification settings - Fork302
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
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 disable 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 changing 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 have |
BetaWas this translation helpful?Give feedback.
All reactions
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
-
Thanks for bringing this up. It seems that in DJA we have not covered the use case of As a workaround though, what you can do is to create your ownResourceRelatedField and overwrite This could look like the following: classStringResourceRelatedField(ResourceRelatedField):defget_resource_id(self,resource):returnstr(resource) Using such a field would have the same effect as |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1