从Many2ManyField获取查询集(包括相关字段)

发布于 2024-09-05 22:07:58 字数 889 浏览 4 评论 0原文

嘿,我有一个包含一块板的模型,

class Board(models.Model):
    parent_board = models.ForeignKey('self', blank=True, null=True)

每个板都可以属于另一个板,

所以可以说

Linux
Windows
OS X

可以属于一个名为的板,

Computing

这些板包含一个线程对象

class Thread(models.Model):
    board = models.ForeignKey(Board)

现在,假设我将一个线程分配给 Windows 板,我可以轻松获得这个对象。

但我想列出与计算相关的所有线程。

该线程属于Windows板,但通过关联它也将属于计算板。

我如何在计算板上运行查询并从其子板检索所有线程(以及仅属于计算线程的任何线程)?

我已经做到了,但它非常粗糙,我想知道是否有更多的 Django 方法来做到这一点

这是我目前的代码(工作)

listings = [] # blank list to hold clean values
for board in board.board_set.all(): # for each board 
    for listing in board.listing_set.all(): # get the listing from each board
        listings.append( listing ) # append to the listings list

Hay, i have a model which houses a board

class Board(models.Model):
    parent_board = models.ForeignKey('self', blank=True, null=True)

Each board can belong to another board

So say

Linux
Windows
OS X

can belong to a board called

Computing

These boards house a Thread object

class Thread(models.Model):
    board = models.ForeignKey(Board)

Now, say i assign a Thread to the Windows board, i can get this object easily.

But i want to list all Threads associated with Computing

The Thread belongs to the Windows board, but it will also belong to the Computing board through association.

How can i run a Query on the computing board and retrieve all threads from it's subboards (as well as any belonging to just the Computing thread)?

I've done it, but it's very crude, i wonder if there a more Django way of doing it

Heres my code of it at the moment (working)

listings = [] # blank list to hold clean values
for board in board.board_set.all(): # for each board 
    for listing in board.listing_set.all(): # get the listing from each board
        listings.append( listing ) # append to the listings list

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

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

发布评论

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

评论(1

晨曦慕雪 2024-09-12 22:07:58

[您的 qn 显示“Thread”模型,但随后继续引用您的工作环代码中的“listing_set” - 我认为这是一个拼写错误?]

您可以使用 Q 对象。假设您的 Board 模型有一个包含板名称的“name”字段,我相信以下内容应该有效:

from django.db.models import Q
Thread.objects.filter(Q(board__parent_board__name='Computing') | Q(name='Computing'))

第一个 Q 对象选择属于板的一部分的线程,该板将“parent_board”设置为名称为“Computing”的板。第二个 Q 对象选择直接属于名为“计算”的板的一部分的线程。

[Your qn shows a 'Thread' model but then goes on to refer to 'listing_set' in your wokring code - I assume this is a typo?]

You could use Q objects. Assuming that your Board model has a 'name' field containing the board name, I believe the following should work:

from django.db.models import Q
Thread.objects.filter(Q(board__parent_board__name='Computing') | Q(name='Computing'))

The first Q object selects threads which are part of a board which has 'parent_board' set to a board with name 'Computing'. The second Q object selects threads which are directly part of a board which has the name 'Computing'.

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