DRF。 NESTED SERIALIZERS PYTHON。对象没有属性

时间:2017-08-23 05:56:08

标签: python django

我在这里测试了我的django应用程序:

class GiftEnSerializerTest(APITestCase):
    def test_if_GiftEnSerializer_return_required_fields(self):
        data = {
            'gift': '1', 
            'name': 'Neapoletan',
            'description': 'A very delecios pizza!', 
            'short_description': 'Eat our pizza and be happy!', 
            'categories': 'Pizzas', 
            'partner': 'PizzaMania', 
            'addresses': 'Miron Costin 15/3'
        }
        serializer = GiftEnSerializer(data)
        self.assertEqual(serializer.data, "")

当我尝试运行它时,它会给我错误 - AttributeError: 'dict' object has no attribute 'gift'

这是序列化器:

class PhotoSerializer(DynamicFieldsModelSerializer):
    picture_url = serializers.SerializerMethodField()

    class Meta:
        model = GiftPicture
        fields = ('picture', 'picture_url')

    def get_picture_url(self, obj):
        return self.picture.url

模特:

class GiftPicture(models.Model):
    gift = models.ForeignKey(Private, related_name='gift_picture', on_delete=models.CASCADE)  
    picture = models.ImageField(upload_to="gift_galery")

这是GiftEn模型:

class GiftEn(models.Model):
    gift = models.ForeignKey(Private, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    description = models.TextField(max_length=1000)
    short_description = models.TextField(max_length=500)
    categories = models.ManyToManyField(CategoryEn,)
    partner= models.ManyToManyField(Partner,)
    addresses= models.ManyToManyField(AddressWhereTakeGift,)

    def __str__(self):
        return '%s, %s, %s' % (self.name, self.description, self.short_description)

    class Meta:
        ordering = ('-gift__id',)

有人可以帮我解决一些问题吗?

0 个答案:

没有答案