【问题标题】:Django ORM replacement for extra whereDjango ORM 替换额外的 where
【发布时间】:2020-11-06 05:05:45
【问题描述】:

这是我目前计划使用 .extra(where=...) 调用解决的一个场景:

class CustomData(models.Model):
   data = models.CharField()
   target_type = models.ForeignKey(ContentType)
   target_object_id = models.PositiveInteger()
   target_object = GenericForeignKey('target_type', 'target_object_id')

# list of tuples with object ID and mixed content type ID [(1, 15), (2, 15), (3, 11), (4, 12), ...]
related_objects = get_list_of_objects(...)
releated_custom_data = CustomData.objects.extra(
      where=['(target_object_id, target_type_id) = ANY (VALUES %s)'], 
      params=str(related_objects)[1:-1]
)

基于 django docs 的额外功能将在未来被弃用。使用 ORM 进行值对过滤是否有明智的选择?

【问题讨论】:

    标签: python django django-orm


    【解决方案1】:

    您可以创建一个Q 对象来过滤两者的组合,例如:

    from django.db.models import Q
    
    data = [(1, 15), (2, 15), (3, 11), (4, 12)]
    q_filter = Q(
        *[Q(target_object_id=d1, target_type_id=d2) for d1, d2 in data],
        _connector=Q.OR
    )
    
    CustomData.objects.filter(q_filter)

    【讨论】:

    • 谢谢。这会起作用,但这显然会失去在 PostgreSQL 中使用 ANY 的好处,它提供比一系列 OR 子句更好的查询性能。有什么想法吗?
    猜你喜欢
    • 2012-11-14
    • 2011-05-19
    • 1970-01-01
    • 2015-09-20
    • 2022-10-05
    • 2017-07-24
    • 2015-08-21
    • 2013-03-20
    • 2018-12-21
    相关资源
    最近更新 更多