简体   繁体   English

如何使用 Django ORM 查询这个多对多示例?

[英]How do I use the Django ORM to query this many-to-many example?

I have the following models:我有以下型号:

class Author(models.Model):
  author_name = models.CharField()

class Book(models.Model):
  book_name = models.CharField()

class AuthorBook(models.Model):
  author_id = models.ForeignKeyField(Author)
  book_id = models.ForeignKeyField(Book)

With that being said, I'm trying to emulate this query using the Django ORM (select all of the books written by a specific author, noting that Authors can have many books and Books can have many Authors):话虽如此,我正在尝试使用 Django ORM 模拟此查询(选择特定作者写的所有书籍,注意作者可以有很多书,书籍可以有很多作者):

SELECT book_name 
FROM authorbook, book
WHERE authorbook.author_id = 1
AND authorbook.book_id = book.id

I've readthis FAQ page on the Django website, but before I modify my model structure and remove AuthorBook, I was curious if I could emulate that query using the current structure.我已经在 Django 网站上阅读了这个 FAQ 页面,但是在我修改我的模型结构并删除 AuthorBook 之前,我很好奇我是否可以使用当前结构模拟该查询。

You should be able to do:你应该能够做到:

books = Book.objects.filter(authorbook__author_id=1)

to get a QuerySet of Book objects matching your author_id restriction.获取与您的 author_id 限制匹配的 Book 对象的 QuerySet。

The nice thing about Django is you can cook this up and play around with it in the shell. Django 的好处是您可以将其编写好并在 shell 中使用它。 You may also find http://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships to be useful.您可能还会发现http://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships很有用。

"AuthorBook" seems not correctly modeled. “AuthorBook”似乎没有正确建模。

You should use a ManyToManyField :您应该使用ManyToManyField

class Book(models.Model):
  name = models.CharField()
  authors = models.ManyToManyField(Author)

Then you can do:然后你可以这样做:

books = Book.objects.filter(authors__id=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何改进这种多对多的Django ORM查询和模型集? - How can I improve this many-to-many Django ORM query and model set? 如何在Django中使用会员资格访问多对多属性 - How do i access the properties of a many-to-many with Membership in Django 如何在 SQLAlchemy ORM 中查询多对多关系? - How to query a many-to-many relationship in SQLAlchemy ORM? 如何使用小马orm以多对多关系加载数据? - How do I load data in many-to-many relation using pony orm? 如何在SQLAlchemy(ORM)中对3个表的多对多关系进行建模? - How do I model a many-to-many relationship over 3 tables in SQLAlchemy (ORM)? 如何在一对多关系中为反向关系编写Django ORM查询? - How do I write a Django ORM query for the reverse relationship in a one-to-many relationship? 如何做多对多的Django查询找到2位给定作者的书? - How to do many-to-many Django query to find book with 2 given authors? 在Django中,如何在没有显式查询的情况下从多对多关系中的额外字段中检索数据? - In Django, how do you retrieve data from extra fields on many-to-many relationships without an explicit query for it? 如何使用Django Haystack EdgeNGrams与多对多字段? - How to use Django Haystack EdgeNGrams with Many-to-Many field? 如何在表单中使用django admin多对多字段 - how to use django admin Many-to-many field in forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM