Check if all boolean values are the same (database)

17 views
Skip to first unread message

Victor Porton

unread,
Feb 14, 2017, 6:02:56 PM2/14/17
to django...@googlegroups.com
# May contain several purchases
class Transaction(models.Model):
    name = models.CharField(null=True)

class Purchase(models.Model):
    bundle = models.ForeignKey(Transaction, null=True)
    recurring = models.BooleanField(default=False)

For a given transaction, all its purchases must either all be recurring
or all non-recurring.

What is the best way to check it (and throw an exception if in a
transaction there are both non-recurring and recurring purchases)?

Shawn Milochik

unread,
Feb 14, 2017, 7:10:55 PM2/14/17
to django...@googlegroups.com
I'd go with form validation -- you shouldn't allow a Purchase to be added to a transaction if a Purchase with a conflicting status is already in there, nor should a Purchase be able to be modified if it's in a transaction with another Purchase.

Then you never have to check whether a transaction is valid. The models pasted don't give the complete picture of the relationship, but garbage in, garbage out, so just don't allow them to exist in the first place.
Reply all
Reply to author
Forward
0 new messages