Is there any easier way to check if the query set have same value in specific field
class Subject(models.Model):
name = models.CharField(max_length=15, blank=False)
summary = models.CharField(max_length=200, blank=True)
price = models.DecimalField(max_digits=6, decimal_places=2)
is_finish = models.BooleanField(default=False)
y = Subject.objects.all()
how to know if y objects, each of them have is_finish == True without using for loop? I just want to know that the queryset is giving me the same value for is_finish field.
CodePudding user response:
They are several answers to your question:
Option 1:
y = Subject.objects.all()
graduate = all(x.is_finish for x in y)
Option 2:
y = Subject.objects.values_list('is_finish', flat=true)
graduate = all(y)
Option 3:
y = Subject.objects.exclude(is_finish=True).exists()
graduate = not y
Option 4:
y = Subject.objects.filter(is_finish=False).exists()
graduate = not y
Take the option matches better on your code.