Django GenericRelation仍然无法从GenericForeignKey

时间:2016-04-10 20:35:15

标签: python django

在我的Django项目中,我有一个名为Value的模型,它具有GenericForeignKey:

class Value(models.Model):
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, blank=True, null=True)
    val_id = models.PositiveIntegerField(blank=True, null=True)
    data_obj = GenericForeignKey('content_type', 'val_id')

Value是一种多态表,它使用ContentType和GenericForeignKey来指向包含实际数据的任意表。例如,Int可以指向的模型Value

class Int(models.Model):
    data = models.IntegerField(blank=True, null=True)

因此,在创建Int的实例后:

myint = Int.objects.create(data=1)

我可以创建指向它的Value

myval = Value.objects.create(data_obj=myint)

还有Value指向的其他类似模型,例如UIntStringFloat;他们都只有一个字段data。但是,我想知道如何根据字段Value指向的模型所包含的数据来查询/过滤data_obj的实例。即,我希望能够做到这样的事情:

Value.objects.filter(data_obj__data=1)

但这会导致错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 790, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 808, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1243, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1269, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1149, in build_filter
    lookups, parts, reffed_expression = self.solve_lookup_type(arg)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1035, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1316, in names_to_path
    "adding a GenericRelation." % name
FieldError: Field 'data_obj' does not generate an automatic reverse relation and therefore cannot be used for reverse querying. If it is a GenericForeignKey, consider adding a GenericRelation.

问题是即使我已经更新了每个数据模型,以便将GenericRelation字段恢复为Value模型:

class Int(models.Model):
    data = models.IntegerField(blank=True, null=True)
    val_obj = GenericRelation(Value, object_id_field="val_id")

class UInt(models.Model):
    data = models.PositiveIntegerField(blank=True, null=True)
    val_obj = GenericRelation(Value, object_id_field="val_id")

class String(models.Model):
    data = models.CharField(max_length=2048)
    val_obj = GenericRelation(Value, object_id_field="val_id")

# etc.

我仍然无法进行反向查询工作并从上面获得相同的错误。我在这里做错了,添加GenericRelation仍然不允许反向查询或似乎改变任何东西?我肯定希望反向查询能够正常工作,因为项目有一个过滤脚本,如果我只能成功地反向查询,那么在Values上工作会更容易。

更新 :我想出了如何获取FieldError,但仍无法以我想要的方式进行反向查询。我所做的是为每个related_query_nameIntUInt等数据模型添加Float。 GenericRelation字段:

val_obj = GenericRelation(Value, object_id_field="val_id", related_query_name="val")

根据Django文档,我相信我会像这样反转查询:

Value.objects.filter(val__data=1)

然而,即使我确实拥有数据字段包含int 1的数据模型,这个和任何其他反向查询都会返回一个空列表。还有别的我做错了/失踪吗?

3 个答案:

答案 0 :(得分:6)

我遇到了同样的问题,发现我必须为每个相关模型定义一个唯一的related_query_name,如下所述:

https://stackoverflow.com/a/36166644/1143466

例如:

class Int(models.Model):
    data = models.IntegerField(blank=True, null=True)
    val_obj = GenericRelation(Value, object_id_field="val_id",
                              related_query_name="int")

class UInt(models.Model):
    data = models.PositiveIntegerField(blank=True, null=True)
    val_obj = GenericRelation(Value, object_id_field="val_id",
                              related_query_name="uint")

class String(models.Model):
    data = models.CharField(max_length=2048)
    val_obj = GenericRelation(Value, object_id_field="val_id",
                              related_query_name="string")

然后,您将通过分别查询每个模型来创建过滤器,例如:

from django.db.models import Q

val = 1
val_filter = Q(int__data=val) | Q(uint__data=val) | Q(string__data=val)
filtered_values = Value.objects.filter(val_filter)

答案 1 :(得分:0)

GenericRelation不是实际字段;什么都不需要添加到数据库;这不是你错误的原因。

您可能需要将定义模型的应用添加到INSTALLED_APPS设置。

答案 2 :(得分:0)

@LeilaHC的答案中指出,对GenericForeignKey或GenericRelation的调用中可能有一个参数,该参数允许您更改通用FK的名称,但是如果更改此名称,则会出现问题。描述多态关系的ID和模型的字段(至少直到Django 1.9为止)。即content_typeobject_id。因此

class Value(models.Model):
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, blank=True, null=True)
    val_id = models.PositiveIntegerField(blank=True, null=True)
    data_obj = GenericForeignKey('content_type', 'val_id')

应该是

class Value(models.Model):
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, blank=True, null=True)
    object_id = models.PositiveIntegerField(blank=True, null=True)
    data_obj = GenericForeignKey('content_type', 'object_id')

问题有时具有反向关系的性质,有时您会收到“字段'XXX'不存在。有效的选择是...,object_id,content_type,...”错误(其中XXX是您将其更改为的名称)在django.contrib.contenttypes中。