class A(models.Model):
pass
class B(models.Model):
a = models.ForeignKey(A)
content_type = models.ForeignKey(ContentType)
object_id = models.IntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
如何获取 A 实例,通过其参数与一些 B 实例链接。我尝试这样做:
instances = { '1':10, '2':20, '3':30 }
for ct, id in instances.items():
qset |= Q(content_type=int(ct), object_id=int(id))
a = A.objects.all().select_related().filter(qset)
没有错误:«Cannot resolve keyword 'object_id' into field。» 我可以通过链接 B 获得 A 什么?
谢谢!
[PS] 现在这个工作,但不完全是它应该:
a_all = A.objects.all()
for a in a_all:
print a.a_set.filter(qset)