Django 多对多插入排序 [英] Django Many To Many Insert Ordering

查看:37
本文介绍了Django 多对多插入排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们为此苦苦挣扎了几天,并在网络上进行了大量搜索.

We have been struggling with this for a few days and have done lots of searches on the web.

我们正试图弄清楚如何在 Django 表单中为多对多字段保存条目.

We are trying to figure out how entries are saved in Django forms for many to many fields.

例如,我们有一个与图像有多对多关系的新闻模型.当我们将图像添加到新闻文章时,例如id 为 10,2,14 的图片我们可以在保存新闻文章表单时看到 post 值如下:

For example we have a news model that has a many to many relationship with images. When we add images to a news article e.g. images with id 10,2,14 we can see the post values are the following when saving a news article form:

  • 照片 10
  • 照片 2
  • 照片 14

当我们查看多对多交集表时,顺序并未保留.我们看不出插入照片的顺序有什么逻辑.

When we look in the many to many intersection table the order has not been preserved. We can see no logic in the order the photos are inserted.

希望这是有道理的!

非常感谢您提前回答.

阿里夫

推荐答案

Django 不保证保留多对多关系的顺序.如果你需要这个,你应该使用 through 带有显式订单"字段的表.

Django doesn't guarantee to preserve the order of many-to-many relationships. If you need this, you should use a through table with an explicit 'order' field.

这篇关于Django 多对多插入排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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