我試圖用文檔(http://www.django-rest-framework.org/api-guide/relations)序列化與RelatedFields的模型,但我不斷收到AttributeError。Django-REST序列化相關字段
的錯誤:
AttributeError at /testapi/foo/
'Foo' object has no attribute 'bar1'
型號:
class Foo(models.Model):
foo_id = models.AutoField(primary_key=True)
name = models.TextField()
zip_code = models.TextField()
class Bar(models.Model):
user = models.OneToOneField(User)
arbitrary_field1 = models.ForeignKey(Foo, related_name='bar1')
arbitrary_field2 = models.ForeignKey(Foo, related_name='bar2')
的串行:
class FooSerializer(serializers.ModelSerializer):
bar1 = serializers.RelatedField()
bar2 = serializers.RelatedField()
class Meta:
model = Foo
fields = (
'foo_id',
'name',
'zip_code',
'bar1',
'bar2',
)