Goal: Set customer_number
to a calculated value of b_country
+ id
+ 10000.
models.py
class Customer(models.Model):
id = models.BigAutoField(primary_key=True)
customer_number = models.CharField("Customer #", max_length=10, default=0)
b_country = models.TextField("Country", max_length=2, choices=COUNTRY_OPTIONS)
@property
def get_customer_number(self):
a = self.b_country
b = (self.id + 10000)
return a + str(b)
def save(self, *args, **kwarg):
self.customer_number = self.get_customer_number
super(Customer, self).save(*args, **kwarg)
Issue: When the form is submitted to create a new customer (new record) then I get the following error:
a 'CA'
self Error in formatting: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'>
When I already have a record (updating and existing Customer's info) then the code works fine. It is my understanding that it isn't able to get the id
as it is still validating the data before being sent to the model, hence no Customer.id
to pull for this instance.
Question: Is there a cleaner way of setting a customer_number
that is based on the PK?