我有类Streams
在我的型号.py它应该具有另一个类类型(Commentdata
或ResponseData
)的泛型外键
当我得到一个Streams行时,我想访问另一个具有相同id的类中的数据。例如,当我创建一个Stream db条目时,它还将创建一个具有相同id的CommentData db条目,并且该行将具有一个到CommentData
的genericforeignkey。当我想要访问注释数据时,我将检查该流的相关类,然后查询id=流id你知道吗
这是我的课程:
limit = models.Q(app_label='picture', model='commentdata') | models.Q(app_label='picture', model='responsedata')`
content_type = models.ForeignKey(ContentType, verbose_name='Data Type', limit_choices_to=limit, null=True, blank=True)
object_id = models.PositiveIntegerField(verbose_name='Related Object', null=True, blank=True)
content_object = GenericForeignKey('content_type', 'object_id')
在django admin中,我可以将Streams
内容类型保存为(Commentdata
或ResponseData
),这很好。你知道吗
如果我按照
x = Streams.objects.all()
y = x[0].content_type
我可以输出与流相关的类。当我得到错误时,我不能像y.objects.all()
那样获取相关类
Manager isn't accessible via ContentType instances
是否仍有使用ContentType管理器查找此信息的方法?ContentType.objects.get_for_model(x[0])
返回类Stream
。类似地ContentType.objects.get_for_model(y)
返回内容类型。你知道吗
谢谢
好的。我知道你在做什么。首先,使用content\u types API无法完成您正在尝试的操作。但这是可以做到的。你知道吗
正如你所说:
所以你需要像这样使用基本的Django ORM:
相关问题 更多 >
编程相关推荐