Django 外键的复杂查询
我在同一个应用程序中有两个模型。该应用程序称为“新闻”,它的模型中有两个类,称为“文章”和“类别”。
class Category(models.Model):
name = models.CharField(_("Name"), max_length=100)
slug = models.SlugField(_("Slug"), max_length=100, unique=True)
class Article(models.Model):
category = models.ForeignKey(Category, verbose_name=_("Category"))
archived = models.BooleanField(_("Archive this?"), default=False)
我想创建一个查询,显示所有已存档但按类别分组的文章。
我如何有效地完成这个任务?
I have two models in the same application. The application is called "News", and it has two classes in its model called "Article" and "Category".
class Category(models.Model):
name = models.CharField(_("Name"), max_length=100)
slug = models.SlugField(_("Slug"), max_length=100, unique=True)
class Article(models.Model):
category = models.ForeignKey(Category, verbose_name=_("Category"))
archived = models.BooleanField(_("Archive this?"), default=False)
I want to create a query that shows me all of the articles which are archived but grouped by category.
How would I accomplish this efficiently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我正在编辑此内容以获取更多信息以尝试提供帮助。
给定:
您希望查询集包含什么?
i am editing this to get more info to try and help out.
given:
what would you want your queryset to contain?
这不是你想要的吗?
Isn't this what you want?