added model constraints
This commit is contained in:
parent
a072224645
commit
e35e6414aa
|
@ -1,22 +1,33 @@
|
||||||
from django.db import models, connection
|
from django.db import models, connection
|
||||||
from user.models import User, Institution
|
from user.models import User, Institution
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
|
|
||||||
class StateDefinition(models.Model):
|
|
||||||
institution = models.ForeignKey(Institution, on_delete=models.CASCADE)
|
|
||||||
order = models.PositiveIntegerField()
|
|
||||||
state = models.CharField(max_length=255)
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return f"{self.institution.name} - {self.state}"
|
|
||||||
|
|
||||||
class State(models.Model):
|
class State(models.Model):
|
||||||
date = models.DateTimeField(auto_now_add=True)
|
date = models.DateTimeField(auto_now_add=True)
|
||||||
institution = models.ForeignKey(Institution, on_delete=models.CASCADE)
|
institution = models.ForeignKey(Institution, on_delete=models.CASCADE)
|
||||||
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
|
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
|
||||||
|
|
||||||
state = models.CharField(max_length=255)
|
state = models.CharField(max_length=50)
|
||||||
snapshot_uuid = models.UUIDField()
|
snapshot_uuid = models.UUIDField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
unique_together = ('institution', 'state')
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.institution.name} - {self.state} - {self.snapshot_uuid}"
|
return f"{self.institution.name} - {self.state} - {self.snapshot_uuid}"
|
||||||
|
|
||||||
|
class StateDefinition(models.Model):
|
||||||
|
institution = models.ForeignKey(Institution, on_delete=models.CASCADE)
|
||||||
|
order = models.AutoField(primary_key=True)
|
||||||
|
state = models.CharField(max_length=50)
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
if not StateDefinition.objects.filter(institution=self.institution, state=self.state).exists():
|
||||||
|
raise ValidationError(f"The state '{self.state}' is not valid for the institution '{self.institution.name}'.")
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
self.clean()
|
||||||
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"{self.institution.name} - {self.state}"
|
||||||
|
|
Loading…
Reference in a new issue