如何使用 django Rest 框架通过直通模型序列化 ManyToManyField

2024-06-19

我有一个 Recipe 模型,其中包含 IngredientType 对象的 M2M 字段。该字段(又名成分列表)通过 Ingredient 对象使用臭名昭著的“through”模型,该对象将额外的数据添加到我的 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="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()

我尝试使用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')

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

显然,当我改变线路时:

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

to:

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

也就是说,更改序列化器,它可以工作,但不会获取成分数据。 我使用过这个链接:在 Django Rest Framework 的响应中包含中介(通过模型) https://stackoverflow.com/questions/17256724/include-intermediary-through-model-in-responses-in-django-rest-framework作为参考,但显然它并没有解决我的问题。
任何帮助,将不胜感激。 tnx, 尼特赞


On your Recipe模型,对于ingredient_list领域你有一个ManyToManyField指向IngredientType.

On your RecipeSerializer the ingredient_list字段未使用IngredientTypeSerializer而是IngredientSerializer.

这就是错误。 (它解释了错误消息 - 实际模型source没有序列化器正在查找的属性。)

除此之外,您的命名方案非常混乱。 “配方”很好,但是您所说的“成分类型”可能应该只是“成分”,然后您应该为整个表找到一个不同的名称。 (也许是“食谱成分详细信息”)

我希望这有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 django Rest 框架通过直通模型序列化 ManyToManyField 的相关文章

随机推荐

  • 在 Android 上使用 MediaPlayer 流式传输经过身份验证的视频

    我正在尝试从具有基本身份验证的 SharePoint 服务器 URL 流式传输和播放视频 用户名密码 在我的 Android 设备中browser Media Player VideoView但我得到了 错误 sorry this vide
  • 将 useRef 挂钩传递给 ref 属性的正确方法

    我不确定如何更明确地提出这个问题 但它是关于值传递 and 引用传递反应中的情况 还有胡克斯 我正在使用 gsap 来制作 div 滑入和滑出的动画 这是其上下文 但我猜测 ref 的用途并不重要 因此 这工作得很好 尽管这是一种更典型的类
  • 整个应用程序中的全局“搜索功能”

    在我的整个应用程序中 我希望搜索按钮执行单独的操作Activity 即 当我按下搜索按钮时 从应用程序中的任何位置调用一个单独的活动 有什么方法可以代替定义onSearchRequested 在每项活动中 我只是在一个地方配置它 例如Man
  • GET 数据是否也在 HTTPS 中加密?

    当你拿到时 https crypted google com search q s https encrypted google com search q s Is the s查询已加密 还是只是回应 如果不是 为什么谷歌还要对其公共内容进
  • 确定是否向 Firebase 实时数据库添加或删除数据

    每当添加新帖子时 我都会尝试将通知推送到 Android 应用程序 但是 只要数据 更改 即即使帖子被删除 我不需要 通知也会到达 我如何设置一个条件 以便 FCM 仅在添加帖子时才发送通知 这是我的 index js 文件 const f
  • 使用jquery和css点击按钮后旋转div文本

    我想使用 jquery 和 css 单击按钮后旋转 div 文本 如果用户点击Rotate Left按钮 然后文本在左侧旋转 or用户点击Rotate Right按钮然后文本在右侧旋转 Example div Happy Birthday
  • while 循环中的表并排

    in a while loop its creating a list of heading and image links i want to display it as side by side like in following im
  • 使用宏从 Excel 电子表格中删除任何非指定字符

    我正在尝试通过删除任何非标准字符来清理 Excel 中的 CSV 文件 我唯一关心保留的字符是 A Z 0 9 和一些标准标点符号 任何其他字符 我想删除 当它找到包含我未指定的任何字符的单元格时 我已经得到了以下宏来删除整行 但我不确定如
  • 如何在 Angular 2 karma jasmine 测试中从 JSON 文件加载模拟数据?

    我在写信业力茉莉花测试用例角2 我们遇到了在单独的 JSON 文件中模拟数据的需求 因为数据很大 希望确保代码整洁 为此我进行了很多搜索但没有找到合适的解决方案 我们已经使用以下方式模拟 HTTP 服务模拟后端 所以我们不能使用Angula
  • 启动nodejs时出错:openssl配置失败

    启动 Express 节点时出现以下错误 openssl 配置失败 错误 02001003 系统库 fopen 没有这样的 过程 节点无论如何都会启动 我没有尝试使用 SSL 这是起始代码 app Express app set port
  • PHP 和 MySQL - 高效处理多个一对多关系

    我正在寻求一些有关使用 MySQL 和 PHP 检索和显示数据的最佳方法的建议 我有 3 个表 所有一对多关系如下 Each SCHEDULE有很多覆盖每个覆盖都有很多地点 我想检索这些数据 以便它可以全部显示在单个 PHP 页面上 例如列
  • 正则表达式 - 将 target="blank" 添加到我的内容中的所有 标记链接

    有人可以帮我在 C net 中创建一个正则表达式来添加target blank to all a 在我的内容中标记链接 如果链接已经设置了目标 则将其替换为 blank 目的是在新窗口中打开我的内容中的所有链接 感谢你的帮助 dotnet岩
  • Oracle:如何获取刚刚插入的行的序列号?

    如何获取刚刚插入的行的序列号 插入 返回 declare s2 number begin insert into seqtest2 x values aaa returning seq into s2 dbms output put lin
  • 使用 Hive 计算文本变量的单词频率

    我有一个变量 每一行都是一个句子 例子 Row1 Hey how are you Rwo2 Hey Who is there 我希望输出是按单词分组的计数 Example Hey 2 How 1 are 1 我正在使用 split a bi
  • 在 React 应用程序中简单连接到 mongodb

    我使用 create react app 创建了简单的反应应用程序 这个应用程序包含表单 验证和引导程序 没有什么花哨的东西能像魅力一样发挥作用 我还注册了 mongo 以获得免费集群 以便我可以发送一些数据 所以我有这个网址 mongod
  • &(与符号)和 && 或 | 之间的区别(管道)和||在 Objective-C 中?

    我想知道Objective C是否关心我是写 还是 我相信一个与号 会或应该导致如果左侧已经为假 则右侧将不会被评估 这适用于 Objective C 吗 Yes 这些运算符在 C 和 Objective C 中的功能相同 就像在 C 中一
  • Grep 批量 ping

    寻找一种更好的方法来做到这一点 而不是我习惯的 手动 方法 因为这是一个我必须定期经历的过程 我有一系列要 ping 的 IP 从10 0 1 15 to 10 0 50 15 第三个八位位组指的是物理位置 最后一个八位位组指的是该位置处的
  • 使用 docker for windows 工具箱切换到 Windows 容器

    我已经在 Windows 7 64 位操作系统上安装了 docker for windows toolbox 我无法使用 docker 菜单切换到 Windows 容器 因为 docker 图标在系统托盘中不可用 Docker 服务也不可用
  • 如何将配置文件添加到 Eclipse 中的默认 Maven 目标?

    我在 Eclipse 中使用 Maven 来构建和部署代码 选择时 Context Menu gt Run As 几个Maven目标如下 据我所知 如果我想创建自定义构建配置 我会单击 运行配置 来创建自定义配置 然而 似乎没有办法修改菜单
  • 如何使用 django Rest 框架通过直通模型序列化 ManyToManyField

    我有一个 Recipe 模型 其中包含 IngredientType 对象的 M2M 字段 该字段 又名成分列表 通过 Ingredient 对象使用臭名昭著的 through 模型 该对象将额外的数据添加到我的 IngredientTyp