Created
January 4, 2017 16:36
-
-
Save ddahan/19f0961aeeb4bdb4d07a476174b9219f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# models.py | |
class Points(TimeStampedModel): | |
class Meta: | |
# Empêche les doublons au niveau DB (sécurité) | |
unique_together = ('benef_card', 'spendable_at') | |
# ... | |
def save(self, *args, **kwargs): | |
''' Permet de ne pas dupliquer deux entrées de Points ayant le même | |
couple (owner, card) ''' | |
if self.pk: # save() appelée dans un update, on garde le cpt normal | |
super(Points, self).save(*args, **kwargs) | |
else: # save() appelée dans un create, on modifie le cpt | |
if Points.objects.filter(benef_card=self.benef_card, | |
spendable_at=self.spendable_at).exists(): | |
pass # On ne fait rien si le couple existe déjà | |
else: | |
super(Points, self).save(*args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment