我有这两个模型:
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):
)。
我需要从comm
中选择ModelInteractions
id2
= id
(根据要求收到ID)。
当前代码返回:
Cannot resolve keyword 'ModelInteractions' into field. Choices are: id, name
答案 0 :(得分:1)
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()
interaction= models.ForeignKey(ModelInteractions,on_delete=models.CASCADE)
制作这样的模型,然后制作迁移和迁移
然后运行相同的查询,直到这里
condition = Q(name__icontains=names[0])
for name in names[1:]:
condition &= Q(name__icontains=name)
然后改变这一行
condition &= Q(interaction__id2=id)
然后
models = Models.objects.filter(condition)