【问题标题】:Django combine foreign keys in a single querysetDjango 在单个查询集中组合外键
【发布时间】: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


    【解决方案1】:

    目前我面前没有 Django,但是像这样的东西呢:

    class Person(models.Model):
        pass
    
    
    class Book(models.Model):
        year_published = models.PositiveIntegerField()
        author = models.ForeignKey(Person, related_name='books')
    
    
    class MusicAlbum(models.Model):
        year_published = models.PositiveIntegerField()
        producer = models.ForeignKey(Person, related_name='albums')
    
    Person.objects.filter(books__year_published__gte=2018, albums__year_published__gte=2018)
    

    或者,如果您无论如何都必须执行前两个查询,

    Person.objects.filter(books__in=recent_books, albums__in=recent_music_albums)
    

    【讨论】:

      【解决方案2】:

      您将在Person 模型实例上拥有RelatedManager 用于Books 和MusicAlbums。可能它们仍将具有默认名称 book_setmusicalbum_set,因为您没有覆盖它们。

      您可以使用这些来查找与一个人实例关联的书籍/音乐专辑:

      persons_books = person.book_set.all()
      persons_musicalbums = person.musicalbum_set.all()
      

      同样,您可以从模型管理器生成相关查询集:

      qs = Person.objects.exclude(book=None).exclude(musicalbum=None)
      

      【讨论】:

        【解决方案3】:

        同样可以这样实现:

        person = Person.objects.latest('book__year_published', 'musicalbum__year_published')



        personList = Person.objects.all().order_by('-book__year_published', '-musicalbum__year_published')

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-03-09
          • 2013-09-06
          • 2015-01-25
          • 2013-09-17
          • 2018-08-02
          • 2016-04-07
          • 2013-09-27
          相关资源
          最近更新 更多