如何使用django rest_framework进行序列化一个具有直通模型的ManyToManyFields [英] How to serialize using django rest_framework a ManyToManyFields with a Through Model

查看:116
本文介绍了如何使用django rest_framework进行序列化一个具有直通模型的ManyToManyFields的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配方模型,它保存了一个IngredientType对象的M2M字段。这个字段,也就是ingredients_list,这个臭名昭着的'通过'模型通过一个Ingredient对象添加额外的数据到我的IngredientType。
这些是我的类:

  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 =这张照片将显示
ingredients_list = models.ManyToManyField(IngredientType,through ='Ingredient')

class成分(models.Model):
ingredients_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.Bool我已经尝试使用rest_framework序列化它们:


($)



<

  class IngredientTypeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = IngredientType
fields =('name','复数名','照片','is_main')

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

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

但是当尝试运行它时,我得到一个AttributeError:
'IngredientType'对象没有属性'ingredients_type'



更改行:

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

to:

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

,更改Serializer,它的工作原理,但没有提取我的成分数据。
我已经使用这个链接:
在Django Rest框架中包含回调中的中介(通过模型)
作为参考,但显然没有解决我的问题。

任何帮助将不胜感激。
tnx,
Nitzan

解决方案

在您的食谱 model,对于 ingredients_list 字段,您有一个 ManyToManyField 指向 IngredientType

在您的 RecipeSerializer ingredients_list 字段不使用 IngredientTypeSerializer ,而是使用 IngredientSerializer



这是错误。 (它解释了错误消息 - 中的实际模型没有该序列号查找的属性。)



除此之外,您的命名方案令人大吃一惊。 配方很好,但是你所说的IngredientType应该只是Ingredient,然后你应该找到一个不同的名称。 (也许RecipeIngredientDetails)



我希望有帮助。


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:

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:

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'

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

解决方案

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

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

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")

I hope that helps.

这篇关于如何使用django rest_framework进行序列化一个具有直通模型的ManyToManyFields的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆