Reputation: 35
I have these two models:
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
and I want to select also comm
. In my view, I use the following code to get data from 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
is received on request (def details(request, id):
).
I need to select comm
from ModelInteractions
where id2
= id
(id received on request).
Current code returns:
Cannot resolve keyword 'ModelInteractions' into field. Choices are: id, name
Upvotes: 2
Views: 174
Reputation: 27513
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)
make your models like this, then makemigrations and migrate
then run the same query till here
condition = Q(name__icontains=names[0])
for name in names[1:]:
condition &= Q(name__icontains=name)
then change this line
condition &= Q(interaction__id2=id)
then
models = Models.objects.filter(condition)
Upvotes: 1