Consider this django model setup:
from django.db import models
class Foo(models.Model):
field_foo = models.CharField(max_length=20,
null=False,
blank=False,
unique=True,)
class Bar(models.Model):
field_bar = models.CharField(max_length=20,
null=False,
blank=False,
unique=True,)
class Foobar(models.Model):
field_foo = models.ForeignKey(foo,on_delete=models.CASCADE)
field_bar = models.ForeignKey(bar,on_delete=models.CASCADE)
I want to look for two rows that have the same field_foo
and field_bar
values. I can do this manually, but I want to know if there is a feature of django that takes care of that. The way I do this now is:
for f in Foo.objects.all():
for b in Bar.objects.all():
fb = Foobar.objects.filter(foo=f, bar=b)
if len(fb)>2:
something='do'