我有以下概念模型:
class GenericAbstractBase(models.Model):
name = models.CharField(max_length=255)
staff = generic.GenericRelation(
"Staff",
content_type_field="content_type",
object_id_field="object_id",
)
class Meta:
abstract = True
class GenericModelA(GenericAbstractBase):
extra_a = models.CharField(max_length=255)
class GenericModelB(GenericAbstractBase):
extra_b = models.CharField(max_lenth=10)
class Staff(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
active = models.CharField(max_length=10, choices = ACTIVE_CHOICES)
limit = models.Q(app_label='staff', model='genericmodela') | models.Q(app_label='staff', model='genericmodelb')
content_type = models.ForeignKey(ContentType, limit_choices_to=limit)
object_id = models.PositiveIntegerField()
generic_object = generic.GenericForeignKey("content_type", "object_id")
在 Django v1.4 和 Django v1.5 中,以下查询可以正常工作:
>>> ctype = ContentType.objects.get_for_model(GenericModelA)
>>> Staff.objects.filter(
content_type=ctype,
genericmodela__name__icontains="a"
)
>>> [<Staff: Abbott, Kaylee>, <Staff: Adams, Kay>, ... ]
它产生的 SQL (sqlite) 看起来像:
SELECT
"staff_staff"."id", "staff_staff"."first_name","staff_staff"."last_name",
"staff_staff"."active","staff_staff"."content_type_id" ,"staff_staff"."object_id"
FROM "staff_staff"
INNER JOIN "staff_staff" T2 ON ("staff_staff"."id" = T2."id")
INNER JOIN "staff_genericmodela" ON (T2."object_id" = "staff_genericmodela"."id")
WHERE (
"staff_genericmodela"."name" LIKE % a % ESCAPE \ '\\\'
AND "staff_staff"."content_type_id" = 11
)
但是在 Django 1.6 中,查询失败并出现 FieldError:
FieldError: Cannot resolve keyword 'genericmodela' into field. Choices are: active, content_type, department, first_name, id, last_name, object_id, position
发行说明中的以下声明可能是相关的:
Django 1.6 包含对 ORM 的许多更改。这些变化主要分为三类:
- 错误修复(例如,通用关系的正确连接子句、查询组合、连接提升和连接修剪修复)
我的问题是,Django 1.6 中的哪些变化导致了它的崩溃?我是否坚持extra
在 Python 中使用或执行这种类型的过滤?