如何使用django rest_framework进行序列化?

时间:2021-05-15 19:18:24

I have a Recipe model that holds an M2M field of an IngredientType object. This field, aka ingredient_list, uses the notorious 'through' model via an Ingredient object that adds extra data to my IngredientType. these are my classes:

我有一个配方模型,它拥有一个M2M字段的配料类型对象。这个字段,也就是配料表,通过一个成分对象使用了臭名昭著的“通过”模型,它为我的配料类型添加了额外的数据。这些是我的类:

class Recipe(models.Model):
    user_profile = models.ForeignKey(UserProfile, null=True, blank = True)
    name = models.CharField(max_length=200)
    photo1 = models.ImageField( upload_to = 'images/recipies', help_text="This photo will show by default")
    ingredient_list = models.ManyToManyField(IngredientType,through='Ingredient')

class Ingredient(models.Model):
    ingredient_type = models.ForeignKey(IngredientType)
    recipe = models.ForeignKey(Recipe)
    amount = models.IntegerField()
    units = models.CharField(max_length=4,choices=UNIT, 
                               default=None, null=True, blank = True)

class IngredientType(models.Model):
    name = models.CharField(max_length=200)
    plural_name = models.CharField(max_length=200)
    photo = models.ImageField( upload_to = 'images/ingredients')
    is_main = models.BooleanField()

i've tried serializing them using the rest_framework:

我尝试使用rest_framework对它们进行序列化:

class IngredientTypeSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = IngredientType
        fields=('name', 'plural_name', 'photo', 'is_main')

class IngredientSerializer(serializers.HyperlinkedModelSerializer):
    ingredient_type = IngredientTypeSerializer(source = 'ingredient_type')
    amount =  serializers.Field(source='ingredient_type.amount')
    units =  serializers.Field(source='ingredient_type.units')
    recipe = serializers.Field(source='Recipe.name')
class Meta:
    model = Ingredient
    fields=('amount', 'units')

class RecipeSerializer(serializers.ModelSerializer):
    ingredient_list = IngredientSerializer(source='ingredient_list', many=True, read_only = True)
    class Meta:
        model = Recipe
        fields = ('user_profile', 'name','photo1','ingredient_list')

but when trying to run this, I get an AttributeError : 'IngredientType' object has no attribute 'ingredient_type'

但是当试图运行这个时,我得到了一个AttributeError:“配料类型”对象没有属性“配料类型”

clearly, when I change the line:

显然,当我改变这条线:

ingredient_list = IngredientSerializer(source='ingredient_list', many=True, read_only = True)

to:

:

ingredient_list = IngredientTypeSerializer(source='ingredient_list', many=True, read_only = True)

that is, change the Serializer, it works, but without fetching me the Ingredient data. i've used this link: Include intermediary (through model) in responses in Django Rest Framework as reference, but obviously it hasn't solved my problems.
Any help would be appreciated. tnx, Nitzan

也就是说,改变序列化器,它可以工作,但是不需要获取原料数据。我使用了这个链接:在Django Rest框架中包含中介(通过模型)作为参考,但是显然它还没有解决我的问题。如有任何帮助,我们将不胜感激。tnx,Nitzan

1 个解决方案

#1


6  

On your Recipe model, for the ingredient_list field you have a ManyToManyField pointing to IngredientType.

在您的配方模型中,对于配料表字段,您有许多指向配料类型的tomanyfield。

On your RecipeSerializer the ingredient_list field is not using the IngredientTypeSerializer but rather the IngredientSerializer.

在您的RecipeSerializer上,配料表字段并不是使用配料类型,而是使用配料。

This is the error. (And it explains the error message — the actual model at the source doesn't have the attribute being looked for by the serializer.)

这是错误的。(它解释了错误消息——源中的实际模型没有序列化器查找的属性。)

Beyond this, your naming scheme is massively confusing. "Recipe" is fine, but what you're calling "IngredientType" should probably just be "Ingredient" and then you should find a distinct name for the through table. (Maybe "RecipeIngredientDetails")

除此之外,您的命名方案也非常令人困惑。“配方”很好,但是你所称的“配料类型”应该只是“配料”,然后你应该为通过表找到一个不同的名字。(也许“RecipeIngredientDetails”)

I hope that helps.

我希望有帮助。

#1


6  

On your Recipe model, for the ingredient_list field you have a ManyToManyField pointing to IngredientType.

在您的配方模型中,对于配料表字段,您有许多指向配料类型的tomanyfield。

On your RecipeSerializer the ingredient_list field is not using the IngredientTypeSerializer but rather the IngredientSerializer.

在您的RecipeSerializer上,配料表字段并不是使用配料类型,而是使用配料。

This is the error. (And it explains the error message — the actual model at the source doesn't have the attribute being looked for by the serializer.)

这是错误的。(它解释了错误消息——源中的实际模型没有序列化器查找的属性。)

Beyond this, your naming scheme is massively confusing. "Recipe" is fine, but what you're calling "IngredientType" should probably just be "Ingredient" and then you should find a distinct name for the through table. (Maybe "RecipeIngredientDetails")

除此之外,您的命名方案也非常令人困惑。“配方”很好,但是你所称的“配料类型”应该只是“配料”,然后你应该为通过表找到一个不同的名字。(也许“RecipeIngredientDetails”)

I hope that helps.

我希望有帮助。