class Meta:
     constraints = [
         QuerysetConstraint(
             name="QC: Disallow age Multi1 filter",
             queryset=M().objects.filter(age=1),
         ),
         QuerysetConstraint(
             name="QC: Disallow age Multi2 filter",
             queryset=M().objects.filter(age=2),
         ),
     ]
示例#2
0
 class Meta:
     unique_together = ("pizza", "topping")
     constraints = [
         # A pizza with more than 5 toppings gets soggy
         QuerysetConstraint(
             name='At most 5 toppings',
             queryset=M().objects.values('pizza').annotate(
                 num_toppings=Count('topping')).filter(num_toppings__gt=5),
         ),
         # This constraint should be self-explanatory for civilized people
         QuerysetConstraint(
             name='No pineapple',
             queryset=M().objects.filter(topping__name="Pineapple")),
     ]
 class Meta:
     constraints = [
         QuerysetConstraint(
             name="QC: Disallow age in or via Q",
             queryset=M().objects.filter(Q(age=1) | Q(age=2)),
         )
     ]
 class Meta:
     constraints = [
         QuerysetConstraint(
             name="QC: Disallow age in list",
             queryset=M().objects.filter(age__in=[1, 2]),
         )
     ]
 class Meta:
     constraints = [
         QuerysetConstraint(
             name="QC: Disallow age=1 via subquery with 7 subqueries",
             queryset=generate_subquery(7),
         )
     ]
 class Meta:
     constraints = [
         QuerysetConstraint(
             name="QC: Disallow age range filter",
             queryset=M().objects.filter(age__range=(1, 2)),
         )
     ]
 class Meta:
     constraints = [
         QuerysetConstraint(
             name="QC: Disallow age AND filter",
             queryset=M().objects.filter(age__gte=1).filter(age__lte=2),
         )
     ]
 class Meta:
     constraints = [
         QuerysetConstraint(
             name="QC: Disallow age=1 via trigger NEW",
             queryset=M().objects.annotate(
                 new_age=RawSQL("NEW.age", ())).filter(new_age=1),
         )
     ]
 class Meta:
     constraints = [
         QuerysetConstraint(
             name="QC: Disallow age>0 via subquery slice",
             queryset=M().objects.annotate(max_age=Subquery(M().objects.all(
             ).values("age").order_by("-age")[:1])).filter(max_age__gte=1),
         )
     ]
 class Meta:
     constraints = [
         QuerysetConstraint(
             name="QC: Disallow age=1 via subquery",
             queryset=M().objects.annotate(
                 collision=Exists(M().objects.filter(age=1))).filter(
                     collision=True),
         )
     ]
 class Meta:
     constraints = [
         QuerysetConstraint(
             name="QC: Disallow age=1 via annotation",
             queryset=M().objects.annotate(disallowed=Value(
                 1, output_field=models.IntegerField())).filter(
                     age=F("disallowed")),
         )
     ]
 class Meta:
     constraints = [
         models.CheckConstraint(name="CC: Disallow age MultiMixed filter",
                                check=~Q(age=1)),
         QuerysetConstraint(
             name="QC: Disallow age MultiMixed filter",
             queryset=M().objects.filter(age=2),
         ),
     ]
 class Meta:
     constraints = [
         QuerysetConstraint(
             name="QC: Disallow age>0 via When",
             queryset=M().objects.annotate(block=Case(
                 When(age=1, then=F("age")),
                 When(age__gt=1, then=Value(1)),
                 default=Value(0),
                 output_field=models.IntegerField(),
             )).filter(block=1),
         )
     ]
 class Meta:
     constraints = [
         QuerysetConstraint(name="QC: Disallow age=1",
                            queryset=M().objects.filter(age=1))
     ]
 class Meta:
     constraints = [
         # Allow only 1 object in table (by using offset 1)
         QuerysetConstraint(name="QC: Allow only 1 object",
                            queryset=M().objects.all()[1:])
     ]
 class Meta:
     constraints = [
         QuerysetConstraint(name="QC: Allow only 0",
                            queryset=M().objects.exclude(age=0))
     ]