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

Answered by sliverc
Jer-Pha asked this question in Q&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.

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.

You must be logged in to vote

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

Comment options

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.

You must be logged in to vote
0 replies
Answer selected by Jer-Pha
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /