【发布时间】:2018-08-20 19:52:42
【问题描述】:
我在 Django 应用程序中有一个模型被多个其他模型引用为 ForeignKey。
我正在寻找一种方法来为这个类的所有对象创建一个单个查询集,这些对象根据某些标准被其余类引用为 ForeignKey。
我什至不确定这是否可能,但我还是想问一下。
class Person(models.Model):
pass
class Book(models.Model):
year_published = models.PositiveIntegerField()
author = models.ForeignKey(Person)
class MusicAlbum(models.Model):
year_published = models.PositiveIntegerField()
producer = models.ForeignKey(Person)
recent_books = Book.objects.filter(year_published__gte=2018)
recent_music_albums = MusicAlbum.objects.filter(year_published__gte=2018)
# How can I create a **single** queryset of the Person objects that are being referenced as `author` in `recent_books` and as `producer` in `recent_music_albums`?
感谢您的宝贵时间。
【问题讨论】:
标签: python django postgresql django-models django-queryset