-
Notifications
You must be signed in to change notification settings - Fork 300
-
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.
Calling StringRelatedField removes these fields from "relationships". If I disable djangorestframework-jsonapi, the standard API shows the string values correctly.
models.py
... class Model_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
from rest_framework_json_api import serializers ... class Model_DSerializer(serializers.ModelSerializer): ... model_a = serializers.StringRelatedField(many=True) model_b = serializers.StringRelatedField() model_c = serializers.StringRelatedField() class Meta: model = Episode fields = [ "model_a", "model_b", "model_c", ]
I've tried changing to_representation() in ModelDSerializer, instead of using StringRelatedField():
def to_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 def __str__(self): set up correctly.
Beta Was this translation helpful? Give feedback.
All reactions
Thanks for bringing this up. It seems that in DJA we have not covered the use case of StringRelatedField 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 own ResourceRelatedField and overwrite get_resource_id to return a string. You can use this field instead of StringRelatedField.
This could look like the following:
class StringResourceRelatedField(ResourceRelatedField): def get_resource_id(self, resource): return str(resource)
Using such a field would have the same effect as StringRelatedField 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 StringRelatedField 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 own ResourceRelatedField and overwrite get_resource_id to return a string. You can use this field instead of StringRelatedField.
This could look like the following:
class StringResourceRelatedField(ResourceRelatedField): def get_resource_id(self, resource): return str(resource)
Using such a field would have the same effect as StringRelatedField but would of course only work with the DJA renderer.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1