【发布时间】:2018-01-07 22:25:05
【问题描述】:
我有这两个模型:
class ModelInteractions(models.Model):
id1 = models.IntegerField(primary_key=True)
id2 = models.IntegerField()
comm = models.TextField(blank=True, null=True)
class Meta:
managed = False
unique_together = (('id1', 'id2'),)
class Models(models.Model):
id = models.IntegerField(primary_key=True)
name = models.TextField()
class Meta:
managed = False
我还想选择comm。在我看来,我使用以下代码从Models获取数据:
condition = Q(name__icontains=names[0])
for name in names[1:]:
condition &= Q(name__icontains=name)
# ↓↓↓ this line is for what I need but it doesn't work
condition &= Q(ModelInteractions__id2=id)
models = Models.objects.filter(condition)
应请求接收id (def details(request, id):)。
我需要从ModelInteractions 中选择comm,其中id2 = id(根据要求收到ID)。
当前代码返回:
Cannot resolve keyword 'ModelInteractions' into field. Choices are: id, name
【问题讨论】:
-
你不能在 django 中这样做,你需要和外键有关系,然后你可以使用它,如果你不想提及关系,你可以使用原始查询
-
你能举个例子吗?
标签: python sql django postgresql django-queryset