I was studying some Django, and came across a small problem.
I am looking for a way to make suit_length equal to customer's height by default. This kind of code works for me:
class Customer(models.Model):
name = models.CharField(max_length=20)
height = models.IntegerField(default=170)
class Suit(models.Model)
customer = models.ForeignKey(Customer)
suit_design = models.CharField(max_length=100)
suit_length = models.IntegerField(default=0)
def get_height(self):
self.suit_length = self.customer.height
return
But every time I create a new Suit its default suit_length = 0, and I have to run get_height() to get what I want. Is there a way to default suit_length to customer.height and to avoid running get_height() every time I create a new Suit? I am probably looking for smth like this:
class Customer(models.Model):
name = models.CharField(max_length=20)
height = models.IntegerField(default=170)
class Suit(models.Model)
customer = models.ForeignKey(Customer)
suit_design = models.CharField(max_length=500)
suit_length = models.IntegerField(default=lambda:self.customer.height)
But this code doesn't work.