I get the following error in GET /api/stories/169/ in the StorySerializer, noted below in a comment:
AttributeError at /api/stories/169/ 'ManyRelatedField' object has no attribute 'queryset'
After checking the object, I found that if I changed the line from ...
fields['feature'].queryset = fields['feature'].queryset.filter(user=user)
to
fields['photos'].child_relation.queryset = fields['photos'].child_relation.queryset.filter(user=user)
... it looks like it works. But this approach is undocumented, and I'm sure this is the wrong way to do it.
I have these models:
class Story(CommonInfo): user = models.ForeignKey(User) text = models.TextField(max_length=5000,blank=True) feature = models.ForeignKey("Feature", blank=True, null=True) tags = models.ManyToManyField("Tag") class Feature(CommonInfo): user = models.ForeignKey(User) name = models.CharField(max_length=50) class Photo(CommonInfo): user = models.ForeignKey(User) image = ImageField(upload_to='photos') story = models.ForeignKey("Story", blank=True, null=True, related_name='photos', on_delete=models.SET_NULL)
And StorySerializer :
class StorySerializer(serializers.HyperlinkedModelSerializer): user = serializers.CharField(read_only=True) comments = serializers.HyperlinkedRelatedField(read_only=True, view_name='comment-detail', many=True) def get_fields(self, *args, **kwargs): user = self.context['request'].user fields = super(StorySerializer, self).get_fields(*args, **kwargs)
What am I doing wrong? I feel this is related to the direction of the ForeignKey relationship.
source share