I want to have the integer value of one model attribute be the max_length of another model attribute like mentioned below "capacity = models.IntegerField(max_length=Concerthall.capacity)".
class Concerthall(models.Model):
name = models.TextField(max_length=254)
capacity = models.IntegerField()
employees = models.IntegerField()
def __str__(self):
return self.name
class Events(models.Model):
name = models.TextField(max_length=254)
capacity = models.IntegerField(max_length=Concethall.capacity)
timeFrom = models.DateTimeField()
timeTo = models.DateTimeField()
concerthallName = models.ForeignKey(Concerthall, on_delete=models.PROTECT, null=True)
Maybe it is also working with validators but I searched for several hours and wasn't able to find any solution to this.
I suggest a different approach, doing the validation in the models clean()
method:
class Events(models.Model):
name = models.TextField(max_length=254)
capacity = models.IntegerField(default=0)
timeFrom = models.DateTimeField()
timeTo = models.DateTimeField()
concert_hall = models.ForeignKey(Concerthall, on_delete=models.PROTECT)
def clean(self):
if self.capacity > self.concert_hall.capacity:
raise ValidationError(
'the capacity of the event cannot exceed the capacity of the hall')