拥有这两个模型:
class Models(models.Model):
id = models.IntegerField(primary_key=True)
name = models.TextField()
genes = models.TextField(blank=True, null=True)
class Meta:
db_table = 'models'
class ModelInteractions(models.Model):
id1 = models.IntegerField(primary_key=True)
id2 = models.ForeignKey('Models')
comm = models.TextField(blank=True, null=True)
class Meta:
unique_together = (('id1', 'id2'),)
我尝试选择comm
(来自ModelInteractions
),还要name
(来自Models
),针对特定request_id
(ID收到请求)。
我正在使用:
# request_genes example = "ab-2;cra-19"
genes = request_genes.split(';')
condition = Q(id2=request_id)
for field in genes:
condition &= Q(models__genes__icontains=field)
models = ModelInteractions.objects.filter(condition)
返回:
Cannot resolve keyword 'models' into field. Choices are: comm, id1, id2, id2_id.
没有for循环,一切正常,但我没有Models
数据。
我错过了什么?
答案 0 :(得分:1)
尝试,
for field in genes:
condition &= Q(id2__genes__icontains=field)
models = ModelInteractions.objects.filter(condition)
Models
是表的名称。执行查询时,请使用field_names作为管理方法filter
的关键字参数。