From e685a35d19861f8ae4f11d23a72008f36c5ec5e6 Mon Sep 17 00:00:00 2001 From: Midgard Date: Wed, 22 Jul 2020 16:16:33 +0200 Subject: [PATCH] Only one registration for you --- events/models.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/events/models.py b/events/models.py index 754ebd8..dabbbfc 100644 --- a/events/models.py +++ b/events/models.py @@ -46,6 +46,15 @@ class EventRegistration(models.Model): user = models.ForeignKey(CustomUser, on_delete=models.CASCADE) state = models.CharField(max_length=1, choices=REGISTRATION_STATE.items()) + + class Meta: + constraints = [ + models.UniqueConstraint( + fields=["event", "user"], name="user can only register once per event" + ) + ] + + def __str__(self): return f"Reservation[{self.user.username}:{self.event.date}:{self.state}]"