Django自递归ManyToManyField过滤查询
我有一个像这样的模型:
class Activity(models.Model):
title = models.CharField(max_length=200)
summary = models.TextField(null=True, blank=True)
tasks = models.ManyToManyField('self', symmetrical=False, null=True, blank=True)
它的工作原理是这样的,一个活动可以链接到自身,父活动称为“活动”,子活动将被称为“任务/任务”
我如何过滤模型获取所有“活动”以及如何过滤模型以获取所有“任务”?
感谢您的所有帮助。
I have a model like so:
class Activity(models.Model):
title = models.CharField(max_length=200)
summary = models.TextField(null=True, blank=True)
tasks = models.ManyToManyField('self', symmetrical=False, null=True, blank=True)
It works like this, an activity can be linked to itself and the parent activity is called an 'Activity' and the child activity/activities will be called 'Task/Tasks'
How do I filter the model to get all the 'Activities' and How do I filter the model to get all the 'Tasks' ?
Thanks for all the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给你:
:)
如果你使用
__is_null=True
,你可以在没有注释的情况下获得更好的性能,但我现在记不起或快速谷歌它的语法。Here you go:
:)
You could do it with better performance without annotation, if you use
__is_null=True
, but I can't recall or quick google it's syntax right now.获取所有顶级活动:
这是获取上面没有父活动的所有活动。
要获取所有任务、子任务等(上面有父活动的任务):
To get all top-level activities:
This is grabbing all activities that have no parent activity above them.
To get all tasks, sub-tasks, etc. (those that have a parent activity above them):