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.

1 Answer 1

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', )
Sign up to request clarification or add additional context in comments.

4 Comments

That actually works. Was confused because the docs use "tracks" rather than "track_set"..
Please update your answer from "gebreken_set" to "gebrek_set". That way I can also upvote it again.
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.
@RoyPrins Read here Reverse relations they mention _set prefix to get backwards relationship that is your case.

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.