Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attribute error Django REST serializing

I'm trying to write serializers for my models, which are inherited from some base classes, but I get Attribute Error. I would appreciate any help.

Base class:

class AbstractTranslationModel(models.Model):
    class Meta:
        abstract = True

    language = models.CharField(max_length=2, choices=LANGUAGES)

    # Translatable fields
    name = models.CharField(max_length=22)
    description = models.TextField(default='')

    def __str__(self):
        return self.name + ' ' + str(self.language)

Subclass:

class SkillTranslation(AbstractTranslationModel):

    class Meta:
        unique_together = (('translation_of', 'language'), )

    # Foreign keys
    translation_of = models.ForeignKey(Skill, related_name='translations',
                                       related_query_name='translation')

Here is serializer for this class:

class SkillTrSerializer(serializers.ModelSerializer):
    class Meta:
        model = SkillTranslation
        fields = ('language', 'name', 'description', )

And here is serializer for a class, that contains that ones:

class SkillSerializer(serializers.ModelSerializer):
    translations = SkillTrSerializer()

    class Meta:
        model = Skill
        fields = ('translations', )

Here is console output:

>>> from skills.models import *
>>> from api.serializers import *
>>> skill = Skill.objects.all().first()
>>> skill.translations.all()
[<SkillTranslation: English skill EN>, <SkillTranslation: Рашн RU>]
>>> skill.translations.all().first()
<SkillTranslation: English skill EN>
>>> skill.translations.all().first().language
'EN'
>>> s = SkillSerializer(instance=skill)
>>> s.data

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

EDIT: Here is additional information

>>> s
SkillSerializer(instance=<Skill: English skill>):
    translations = SkillTrSerializer():
        id = IntegerField(label='ID', read_only=True)
        language = ChoiceField(choices=(('EN', 'English'), ('RU', 'Russian'), ('UA', 'Ukrainian')), required=True)
        name = CharField(max_length=22)
        description = CharField(required=False, style={'base_template': 'textarea.html'})
        translation_of = PrimaryKeyRelatedField(queryset=Skill.objects.all(), required=True)

Thanks, Anton

like image 668
Anton Shkurenko Avatar asked Aug 15 '15 13:08

Anton Shkurenko


1 Answers

translations is a reverse relation on Skill model hence Skill.transations is a one-to-many relationship. In your serializer you defined it as one-to-one relationship which is why DRF cant find any of the attributes since they dont exist on the QuerySet. To fix it you simply need to use many=True:

class SkillSerializer(serializers.ModelSerializer):
    translations = SkillTrSerializer(many=True)
like image 59
miki725 Avatar answered Oct 18 '22 13:10

miki725