Move clean logic to clean() instead of save()

This commit is contained in:
Casper V. Kristensen 2019-11-19 04:18:47 +01:00
parent 9af691f8ca
commit 0ac3bda6b7
Signed by: caspervk
GPG key ID: 289CA03790535054
2 changed files with 3 additions and 5 deletions

View file

@ -26,10 +26,9 @@ class Raid(models.Model):
class Meta:
ordering = ["-date"]
def save(self, *args, **kwargs):
def clean(self):
if self.response_deadline is None:
self.response_deadline = self.date - timedelta(hours=24)
return super().save(*args, **kwargs)
def __str__(self):
return f"{self.title} on {self.date}"

View file

@ -41,14 +41,13 @@ class User(AbstractUser):
ordering = ["rank", "username"]
def clean(self):
if not hasattr(self, "rank"):
self.rank = Rank.objects.last()
if hasattr(self, "main") and self.main.user != self:
raise ValidationError({"main": "Main character must be owned by user."})
@transaction.atomic
def save(self, *args, **kwargs):
if not hasattr(self, "rank"):
self.rank = Rank.objects.last()
if not hasattr(self, "main"):
self.main = Character.objects.create(
user=None,