0

I have the following django models

class Cast(models.Model):
    coordinates = models.PointField()

class CastImage(models.Model):
    image = models.ImageField(upload_to="castimages")
    cast = models.ForeignKey(Cast, blank=True, null=True, related_name='images')

and the following serializer

class CastSerializer(serializers.ModelSerializer):
    images = serializers.RelatedField(many=True)
    class Meta:
        model = Cast
        fields = ('images',)

This all seems quite similar to what can be found in the doc: http://www.django-rest-framework.org/api-guide/relations#relatedfield

but when I test the serializer for the Cast object, although there really are images for those Cast objects (I checked), I only get empty images lists:

"results": [
        {
            "images": []
        }, 
        {
            "images": []
        }, 
        {
            "images": []
        } ]

What am I missing ?

2
  • Shoudln't that be model = CastImage? Commented Aug 21, 2014 at 6:42
  • I edited the question to make it clearer, but no, it's really Cast. I want to show the CastImage instances that have a foreign key pointing at my Cast instances, as explained here: django-rest-framework.org/api-guide/relations#relatedfield Commented Aug 21, 2014 at 9:22

1 Answer 1

1

If you're accessing the images through Cast, my guess would be that your fields are wrong. How about

class CastSerializer(serializers.ModelSerializer):
  images = serializers.RelatedField(many=True)
  class Meta:
    model = Cast
    fields = ('castimage__image',)
Sign up to request clarification or add additional context in comments.

1 Comment

No, I do not think that is the problem: it just does not load the images, which it should, since I use the right "related_name".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.