0

I am having an interesting problem. I am using the ForeignKey call in the relations mananger. I.e. if I want all the objects from a related model known as hamsters the call would be hamsters_set

now here is a working model attached to a serializer everything is working in this implementation.

class SearchCity(models.Model):
    city = models.CharField(max_length=200)

class SearchNeighborhood(models.Model):
    city = models.ForeignKey(SearchCity, on_delete=models.CASCADE)
    neighborhood = models.CharField(max_length=200)

class CityNeighborhoodReadOnlySerializer(serializers.ModelSerializer):
    searchneighborhood_set = SearchNeighborhoodSerializer(many=True, read_only=True)

    class Meta:
        model = SearchCity
        fields = ('pk','city','searchneighborhood_set')
        read_only_fields =('pk','city', 'searchneighborhood_set')

but with this new model in which I am trying to do the same thing, I am getting an attribute error

class Room(models.Model):
    venue = models.ForeignKey(Venue, on_delete=models.CASCADE)
    name = models.CharField(max_length=100, null=True, blank=True)
    online = models.BooleanField(default=False)
    description = models.CharField(max_length=1000, blank=True, null=True)
    privateroom = models.BooleanField(default=False)
    semiprivateroom = models.BooleanField(default=False)
    seatedcapacity = models.CharField(max_length=10, null=True, blank=True)
    standingcapacity = models.CharField(max_length=10, null=True, blank=True)
    minimumspend = models.PositiveSmallIntegerField(blank=True, null=True)
    surroundsoundamenity = models.BooleanField(default=False)
    outdoorseatingamenity = models.BooleanField(default=False)
    stageamenity = models.BooleanField(default=False)
    televisionamenity = models.BooleanField(default=False)
    screenprojectoramenity = models.BooleanField(default=False)
    naturallightamenity = models.BooleanField(default=False)
    wifiamenity = models.BooleanField(default=False)
    wheelchairaccessibleamenity = models.BooleanField(default=False)
    cocktailseatingseatingoption = models.BooleanField(default=False)
    classroomseatingoption = models.BooleanField(default=False)
    ushapeseatingoption = models.BooleanField(default=False)
    sixtyroundseatingoption = models.BooleanField(default=False)
    boardroomseatingoption = models.BooleanField(default=False)
    theaterseatingoption = models.BooleanField(default=False)
    hallowsquareseatingoption = models.BooleanField(default=False)

class RoomImage(models.Model):
    room = models.ForeignKey(Room, on_delete=models.CASCADE)
    order = models.PositiveSmallIntegerField(blank=True, null=True)
    imageurl = models.CharField(max_length=200, blank=True, null=True)


class RoomAndImageSerializer(serializers.ModelSerializer):
    roomimage_set = RoomImageSerializer(many=True)
    class Meta:
        model = Room
        fields = ('name', 'online', 'description','privateroom','semiprivateroom', 'seatedcapacity', 'standingcapacity','minimumspend','surroundsoundamenity','outdoorseatingamenity','stageamenity','televisionamenity','screenprojectoramenity','naturallightamenity','wifiamenity','wheelchairaccessibleamenity','cocktailseatingseatingoption', 'classroomseatingoption','ushapeseatingoption','sixtyroundseatingoption','boardroomseatingoption','theaterseatingoption','hallowsquareseatingoption','roomimage_set')

AttributeError: Got AttributeError when attempting to get a value for field roomimage_set on serializer RoomAndImageSerializer. The serializer field might be named incorrectly and not match any attribute or key on the QuerySet instance. Original exception text was: 'QuerySet' object has no attribute 'roomimage_set'.

rather interesting as the two implementations seem to be the same. Can anyone catch what I am doing wrong?

Walucas
  • 2,549
  • 1
  • 21
  • 44

1 Answers1

0

You need to set your serializer to readonly

class RoomAndImageSerializer(serializers.ModelSerializer):
    roomimage_set = RoomImageSerializer(many=True,read_only=True)
    class Meta:
        model = Room
        fields = ('name', 'online', 'description','privateroom','semiprivateroom', 'seatedcapacity', 'standingcapacity','minimumspend','surroundsoundamenity','outdoorseatingamenity','stageamenity','televisionamenity','screenprojectoramenity','naturallightamenity','wifiamenity','wheelchairaccessibleamenity','cocktailseatingseatingoption', 'classroomseatingoption','ushapeseatingoption','sixtyroundseatingoption','boardroomseatingoption','theaterseatingoption','hallowsquareseatingoption','roomimage_set')
Walucas
  • 2,549
  • 1
  • 21
  • 44
  • will do, i will accept your answer once I am sure everything is working. Thanks again. –  Mar 13 '18 at 21:33