属性错误 Django REST 序列化
Attribute error Django REST serializing
我正在尝试为我的模型编写序列化程序,这些模型是从某些基础 classes 继承的,但是我得到了属性错误。如果有任何帮助,我将不胜感激。
基础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)
子class:
class SkillTranslation(AbstractTranslationModel):
class Meta:
unique_together = (('translation_of', 'language'), )
# Foreign keys
translation_of = models.ForeignKey(Skill, related_name='translations',
related_query_name='translation')
这是 class 的序列化程序:
class SkillTrSerializer(serializers.ModelSerializer):
class Meta:
model = SkillTranslation
fields = ('language', 'name', 'description', )
这里是 class 的序列化程序,其中包含:
class SkillSerializer(serializers.ModelSerializer):
translations = SkillTrSerializer()
class Meta:
model = Skill
fields = ('translations', )
这是控制台输出:
>>> 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'.
编辑:这是附加信息
>>> 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)
谢谢,安东
translations
是 Skill
模型的反向关系,因此 Skill.transations
是一对多关系。在您的序列化程序中,您将其定义为一对一关系,这就是 DRF 无法找到任何属性的原因,因为它们不存在于 QuerySet
上。要修复它,您只需使用 many=True
:
class SkillSerializer(serializers.ModelSerializer):
translations = SkillTrSerializer(many=True)
我正在尝试为我的模型编写序列化程序,这些模型是从某些基础 classes 继承的,但是我得到了属性错误。如果有任何帮助,我将不胜感激。
基础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)
子class:
class SkillTranslation(AbstractTranslationModel):
class Meta:
unique_together = (('translation_of', 'language'), )
# Foreign keys
translation_of = models.ForeignKey(Skill, related_name='translations',
related_query_name='translation')
这是 class 的序列化程序:
class SkillTrSerializer(serializers.ModelSerializer):
class Meta:
model = SkillTranslation
fields = ('language', 'name', 'description', )
这里是 class 的序列化程序,其中包含:
class SkillSerializer(serializers.ModelSerializer):
translations = SkillTrSerializer()
class Meta:
model = Skill
fields = ('translations', )
这是控制台输出:
>>> 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'.
编辑:这是附加信息
>>> 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)
谢谢,安东
translations
是 Skill
模型的反向关系,因此 Skill.transations
是一对多关系。在您的序列化程序中,您将其定义为一对一关系,这就是 DRF 无法找到任何属性的原因,因为它们不存在于 QuerySet
上。要修复它,您只需使用 many=True
:
class SkillSerializer(serializers.ModelSerializer):
translations = SkillTrSerializer(many=True)