- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 Django v1.9.4,后面是 PostgreSQL 9.2.14。具有以下型号:
from django.db import models
from django.contrib.contenttypes.fields import GenericRelation, GenericForeignKey
from django.contrib.contenttypes.models import ContentType
class Foo(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
bar = GenericForeignKey('content_type', 'object_id')
class Bar(models.Model):
foos = GenericRelation(Foo, related_query_name='bars')
class Meta:
abstract = True
class BarX(Bar):
name = models.CharField(max_length=10, default='bar x')
class BarY(Bar):
name = models.CharField(max_length=10, default='bar y')
创建一些实例来演示我的问题:
>>> bar_x = BarX.objects.create()
>>> bar_y = BarY.objects.create()
>>> foo1 = Foo.objects.create(bar=bar_x)
>>> foo2 = Foo.objects.create(bar=bar_y)
>>> foo1.bar.name
u'bar x'
>>> foo2.bar.name
u'bar y'
我无法在 Django 中遍历 GFK,尝试过滤会引发异常,并显示一条消息建议添加 GenericRelation
。但是通过相关查询名称 bars
使用通用关系并不能可靠地工作。例如:
>>> [foo.bar.name for foo in Foo.objects.all()]
[u'bar x', u'bar y'] # in a pure python loop, it's working
>>> Foo.objects.filter(bar__name='bar x')
FieldError: Field 'bar' does not generate an automatic reverse relation and therefore cannot be used for reverse querying. If it is a GenericForeignKey, consider adding a GenericRelation.
>>> Foo.objects.values_list('bars__name', flat=1)
[None, u'bar y'] # but why None is returned in here?
>>> Foo.objects.filter(bars__name='bar x')
[] # why no result here?
>>> Foo.objects.filter(bars__name='bar y')
[<Foo: Foo object>] # but this one works?
我做错了什么?
给 future 读者的警告:GenericRelation
上的模板化 related_query_name
在 Django 1.9 上无法正常工作。
在 Django 1.10 中添加的是 related_query_name now supports app label and class interpolation using the '%(app_label)s' and '%(class)s' strings , 在 fix 之后对于 #25354被合并了。
如果你使用的是 Django 1.10,你可以继续将 GenericRelation
放在抽象基类上,并将其模板化为 related_query_name='%(app_label)s_%(class) s'
以确保跨子类的唯一性。
最佳答案
一般来说,不可能按照您尝试的方式朝这个方向遍历 GenericForeignKey
。 GenericForeignKey
可以指向应用程序中的任何模型,而不仅仅是 Bar
及其子类。出于这个原因,Foo.objects.filter(bar__somefield='some value')
无法知道您此刻想到的目标模型,因此无法判断目标模型具有哪些字段.事实上,在执行此类查询时无法选择要连接的数据库表 - 它可以是任何表,具体取决于 Foo.content_type
的值。
如果你想在连接中使用通用关系,你必须定义一个 GenericRelation
在这种关系的另一端。这样你就可以让 Django 知道它应该在另一边寻找哪个模型。
例如,您可以像这样创建BarX
和BarY
模型:
class BarX(Bar):
name = models.CharField(max_length=10, default='bar x')
foos = GenericRelation(Foo, related_query_name='bar_x')
class BarY(Bar):
name = models.CharField(max_length=10, default='bar y')
foos = GenericRelation(Foo, related_query_name='bar_y')
如果这样做,则可以执行如下查询:
Foo.objects.filter(bar_x__name='bar x')
Foo.objects.filter(bar_y__name='bar y')
但是,您必须选择一个目标模型。这是一个你无法以任何方式真正克服的限制;每个数据库连接都需要提前知道它操作的是哪些表。
如果您绝对需要允许BarX
和BarY
作为目标,您应该能够使用Q
在查询过滤器中明确列出它们。表达式:
Foo.objects.filter(Q(bar_x__name='bar x') | Q(bar_y__name='bar y'))
关于python - 如何在 Django 中遍历一个 GenericForeignKey?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36164654/
我根据文档编写了此类,以便能够对应用程序中具有 id 的任何内容进行投票: class Vote(models.Model): class Meta: unique_toget
我正在尝试在 Django 中将 ForeignKey 转换为 GenericForeignKey。我计划在三个迁移中执行此操作,mig1、mig2、mig3。 迁移1(mig1)有以下代码 clas
我正尝试在 this tutorial 之后实现事件提要. 当相应的对象(即评论本身)已被删除时,我想删除一个事件(即已添加评论)。这似乎没有级联。 有没有办法在不添加 GenericRelation
我正在创建一个 Django 应用程序,其中所有模型都可以按照用户设置的顺序相互关联。我正在使用 GenericForeignKeys 设置所有这些。关键是我需要能够支持这些类型的关系/管理的多个集合
class MyUser(AbstractBaseUser): ... content_type = models.ForeignKey(ContentType, limit_choi
假设我有一个模型 Box与 GenericForeignKey指向 Apple实例或 Chocolate实例。 Apple和 Chocolate ,反过来,拥有 Farm 的 ForeignKeys和
我正在创建一个自定义评论系统,它可以使用 contenttypes GenericForeignKey 将评论附加到任何模型。 class Comment(models.Model): bod
我更改了 GenericForeignKey() 可以引用的某些对象的默认管理器,以便这些对象可能不再出现在该默认管理器中。 我有其他管理器能够找到这些已删除对象,但我看不出有什么办法可以告诉内容类型
好的,所以我的数据库有一个标题/行类型结构,其中有标题模型,其中有许多行模型。许多型号都采用这种结构。我想跟踪哪些 header 模型与哪些行模型相关。 最初,我只是在 header 模型上有一个指向
重要提示:此问题不再相关。 在 Django 1.7 迁移中,我尝试使用以下代码以编程方式创建评论条目: # -*- coding: utf-8 -*- from __future__ import
我想在 Django Rest Framework 中显示相关 GenericForeignKey 的嵌套表示。这是我尝试过的: class ContentRelatedField(serialize
我是 GenericForeignKey 的新手,我无法让它在查询语句中工作。表格大致如下: class Ticket(models.Model): issue_ct = models.For
我如何根据用户的 content_type 选择创建/处理动态表单? 我正在编写一个 View /模板来添加一个对象,该模型持有其他模型的通用键: class MainModel(models.Mo
我有一个模型,它使用通用外键使用“content_type”字段来存储内容类型和“object_id”来存储对象 ID。该模型需要使用 CRUD API 进行操作,而我正在使用 DRF。我有一个模型的
我正在 Django 中开发一个讨论应用程序,它包含主题、帖子、回复和投票。投票使用 Generic Foreign Keys and Content Types以确保用户只能对特定主题/帖子/回复投
下面的模型显示了一个简单的 GenericForeignKey 关系。它以这种方式设置,以允许任何其他模型重用图像。 class Image(models.Model): name = mod
我有一个事件应用程序来记录用户或任何事件操作。因此,为了实现这一点,我必须在我的模型上使用 GenericForeignKeys,因为许多模型可以执行操作。 这是我的模型: class Activit
我有以下内容: target_content_type = models.ForeignKey(ContentType, related_name='target_content_type') tar
我使用了一个投票应用程序(django-ratings,如果这有什么不同的话),它使用 django 的 GenericForeignKey,有一个用户的 ForeignKey,以及几个其他字段,比如
有没有一种方法可以让 GenericForeignKey 具有与foreignkey连接的两个模型上的字段? from django.db import models from django.cont
我是一名优秀的程序员,十分优秀!