7

Staring at my code for quite a while and I keep running into the same error. Funny thing is that I made a similar set of serializers for another part of my model and those work fine.

This is the error that I keep getting:

AttributeError at /onderhoudapi/conditiedeel/.json Got AttributeError when attempting to get a value for field gebreken on serializer ConditiedeelSerializer. The serializer field might be named incorrectly and not match any attribute or key on the Conditiedeel instance. Original exception text was: 'Conditiedeel' object has no attribute 'gebreken'.

serializers.py

class GebrekSerializer(serializers.ModelSerializer):
    class Meta:
        model = Gebrek
        fields = ('naam', 'get_type', 'get_omvang_waarde', 'get_intensiteit_waarde', 'get_ernst_waarde')


class ConditiedeelSerializer(serializers.ModelSerializer):
    gebreken = GebrekSerializer(many=True, read_only=True)

    class Meta:
        model = Conditiedeel
        fields = ('deel', 'conditiescore', 'gebreken', )

models.py

class Conditiedeel(models.Model):
    deel = models.OneToOneField(Deel, null=True, blank=True)
    conditiegroep = models.ForeignKey(Conditiegroep)
    conditiescore = models.IntegerField(choices=CONDITIE_KEUZES)

    #some class methods here


class Gebrek(models.Model):
    naam = models.CharField(max_length=80)
    omvang = models.IntegerField(choices=OMVANG_KEUZES)
    intensiteit = models.IntegerField(choices=INTENSITEIT_KEUZES)
    conditiedeel = models.ForeignKey(Conditiedeel)
    nengebrek = models.ForeignKey(Nengebrek)

    #class methods here

As you can see, the Gebrek class has a foreign relationship to the Conditiedeel class. That should mean I can use a nested relationship like here. I think I followed the example closely, yet I cannot get it to work.

levi
  • 22,001
  • 7
  • 73
  • 74
Roy Prins
  • 2,790
  • 2
  • 28
  • 47

1 Answers1

11

The problem here is that Conditiedeel model has not an attribute called gebreken, remember that you are trying to get backwards relationship objects, so you need to use gebreken_set as field as django docs says. So your serializer should be

class ConditiedeelSerializer(serializers.ModelSerializer):
    gebrek_set = GebrekSerializer(many=True, read_only=True)

    class Meta:
        model = Conditiedeel
        fields = ('deel', 'conditiescore', 'gebrek_set', )
levi
  • 22,001
  • 7
  • 73
  • 74
  • 2
    That actually works. Was confused because the docs use "tracks" rather than "track_set".. – Roy Prins Feb 16 '15 at 06:02
  • Please update your answer from "gebreken_set" to "gebrek_set". That way I can also upvote it again. – Roy Prins Feb 16 '15 at 06:03
  • I was very much convinced that your answer would not solve the issue. Honestly I still do not quite understand why it worked or rather why my previous solution dod not work. I am well aware of the `_set` pattern in the normal Django database api, but the rest-framework doen not seem top rely on it. Both the docs and my previous code do not rely on _set for foreign keys. – Roy Prins Feb 17 '15 at 07:58
  • 1
    @RoyPrins Read here [Reverse relations](http://www.django-rest-framework.org/api-guide/relations/) they mention _set prefix to get backwards relationship that is your case. – levi Feb 17 '15 at 15:52