gpt4 book ai didi

python - Django GenericRelation 仍然没有启用从 GenericForeignKey 的反向查询

转载 作者:太空狗 更新时间:2023-10-30 02:42:25 24 4
gpt4 key购买 nike

在我的 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。但是,我想知道如何根据字段 data_obj 指向的模型所包含的数据,通过 Value 的实例进行查询/过滤。即,我希望能够做这样的事情:

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,但仍然无法按照我想要的方式进行反向查询。我所做的是为每个 IntUIntFloat 等数据模型添加一个 related_query_name GenericRelation 字段:

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

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

Value.objects.filter(val__data=1)

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

最佳答案

我遇到了同样的问题,发现我必须为每个相关模型定义一个唯一的 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)

关于python - Django GenericRelation 仍然没有启用从 GenericForeignKey 的反向查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36535264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com