Django ManyToMany add() 是追加函数吗?

发布于 2024-09-13 23:41:04 字数 597 浏览 1 评论 0原文

如果我的 models.py 在书籍和作者之间具有 ManyToMany 关系,并且对于特定 SampleBook 我执行: Sample_book.authors.add(author1) Sample_book.authors.add(author2) Sample_book.authors.add(author3)

是按照添加顺序存储在 books.authors.all 中的author1、author2 和author3 吗?

即 ManyToMany add() 函数是否类似于追加?如果我尝试在 for 循环中提取值,它们是否会按照最初添加的顺序返回?

继续:

下面收到的答复指出,db/django 对对象存储的顺序不承担责任。

问题是我有一个嵌套排序问题。例如,我将书籍列表发送到模板,使用 order_by 对其进行排序。但模板需要显示所有书籍,以及每本书的所有作者,并对作者进行排序。

由于书籍和作者之间存在多对多关系,因此每本书的作者不一定按顺序存储(因此是我最初的问题)。因此,模板按传递的顺序显示书籍,我使用重新分组作为一种技巧,对从每本书中通过关联检索到的作者进行排序。

有人有更优雅的解决方案吗?

If my models.py has a ManyToMany relationship between books and authors, and if for a particular SampleBook I execute:
Sample_book.authors.add(author1)
Sample_book.authors.add(author2)
Sample_book.authors.add(author3)

are author1, author2, and author3 stored in books.authors.all in the order in which they were added?

i.e. is the ManyToMany add() function similar to an append? If I try to extract the values in a for loop, will they be returned in the order they were initially added?

Continued:

The answer received below stated that the db/ django did not bear responsibility for the order in which the objects were stored.

The problem is that I have a nested sort problem. e.g. I send over a list of books to the template, using order_by to sort it. But the template needs to display all the books, as well as all authors for each book, with the authors sorted as well.

Since there is a ManyToMany relationship between books and authors, the authors for each book are not necessarily stored in order (hence my original question). So the template displays books in the order passed, and I used regroup as a hack to sort the authors retrieved by association from each book.

Does anybody have a more elegant solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-09-20 23:41:04

关系数据库不保证顺序。

Django 也无法做出保证,因为它取决于底层关系数据库。

如果您想要特定的排序,则必须通过提供某种序列号来实现该特定的排序。


通常 order_by 最简单,因为它是查询集的一部分。请参阅 http://docs.djangoproject.com/ en/1.2/ref/models/querysets/#order-by-fields

最快的方法是创建一个列表并在视图函数中使用sorted

object_list = sorted( some_query_set, key=lambda o: o.some_field )

或者

object_list= list( some_query_set )
object_list.sort( key=lambda o: o.some_field )

其中任何一个都会非常快。

The relational database makes no guarantee of ordering.

Django can't make a guarantee, either, since it depends on the underlying relational database.

If you want a specific ordering, you must implement that specific ordering by providing some kind of sequence number.


Usually order_by is simplest, Since it's part of the query set. See http://docs.djangoproject.com/en/1.2/ref/models/querysets/#order-by-fields

The fastest way is to create a list and use sorted in the view function.

object_list = sorted( some_query_set, key=lambda o: o.some_field )

Or

object_list= list( some_query_set )
object_list.sort( key=lambda o: o.some_field )

Either of these will be really fast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文