gpt4 book ai didi

python - 内部加入 Django 1.9

转载 作者:行者123 更新时间:2023-11-29 12:17:27 25 4
gpt4 key购买 nike

我有这两个模型:

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

最佳答案

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)

关于python - 内部加入 Django 1.9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45431274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com