Django 1.9无法解析关键字'模型'进入田野。选择是:comm,id1,id2,id2_id

时间:2017-08-04 10:43:07

标签: python django postgresql django-1.9

拥有这两个模型:

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数据。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

尝试,

for field in genes:
    condition &= Q(id2__genes__icontains=field)
models = ModelInteractions.objects.filter(condition)

Models是表的名称。执行查询时,请使用field_names作为管理方法filter的关键字参数。