【发布时间】: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