I'm trying to set a conditional unique constrain to one of my model field (column on db perspective) with respect to another boolean field but can't find a way to do it.
I'm creating a web management tool, to aid/control custumers atendance, based on numbered cards while they are still on the store.
My main objective is to prevent my user to save an card number while this number is still active (aka boolean field) with another custumer.
Something like this on models.py:
class Cards(models.Model):
card_number = models.PositiveIntegerField("""{{RESTRICTION NEEDED}}""")
card_date = models.DateTimeField(auto_now=False, auto_now_add=True)
custumer = models.ForeignKey(Customer)
status = models.BooleanField(default=False)
maybe something like this would work directly on postgres, didn't tried yet
CREATE UNIQUE INDEX card_number_ind ON Cards (card_number) WHERE (status is False);
Any chance to do it directly with Django? I mean set a conditional unique constrain with respect to a boolean field (status) on Django?
TIA
Obs.This is my first question ever on stackoverflow, feel free to criticize on style.