Imagine I have those two models:
class A(models.Model):
name = models.CharField(max_length=150)
class B(models.Model):
a = models.OneToOneField(
to=A, on_delete=models.CASCADE, null=False
)
location = models.CharField(max_length=100)
And I want a queryset of B model to be filtered by the a.name and location,
like this:
select * from B join A
on B.a.pk = A.pk
where A.name="name" and B.location="location";
I tried this but it gives an error:
query=B.objects.filter(a.name=name)
CodePudding user response:
Read the documentation carefully. U can access related fields with double underscore. So in your case dat will be:
query=B.objects.filter(a__name='name', location='location')