0

I have a booking system for bank line : this is my model for the customer:

class Customer(models.Model):
customer_bank        = models.ForeignKey('Bank', on_delete=models.SET_NULL,related_name='coustmer_bank' ,null=True)
customer_branch      = models.ForeignKey('Branch', on_delete=models.SET_NULL,related_name='coustmer_branch',null=True)
booking_id           = models.CharField(max_length=120, blank= True,default=increment_booking_number)
identity_type        = models.ForeignKey('IdentityType',on_delete=models.SET_NULL,related_name='identity_type',null=True)
identity_or_passport_number   = models.CharField(max_length=20)
bank_account_no      = models.CharField(max_length=15)
Done                 = models.BooleanField(default=False)
booking_date_time    = models.DateTimeField(auto_now_add=True, auto_now=False)
Entrance_date_time   = models.DateTimeField(auto_now_add=False, auto_now=True)# Must be modified to work with Entrance Date and Time

def __str__(self):
    return self.booking_id

I need to generate a random value for booking_id field depends on bank_number and the branch_number and the Customer id so how can I do that? help please

Mhadi Ahmed
  • 25
  • 1
  • 8

1 Answers1

1

You can overide the save method of the model

class Customer(models.Model):
customer_bank        = models.ForeignKey('Bank', on_delete=models.SET_NULL,related_name='coustmer_bank' ,null=True)
customer_branch      = models.ForeignKey('Branch', on_delete=models.SET_NULL,related_name='coustmer_branch',null=True)
booking_id           = models.CharField(max_length=120, blank= True,default=increment_booking_number)
identity_type        = models.ForeignKey('IdentityType',on_delete=models.SET_NULL,related_name='identity_type',null=True)
identity_or_passport_number   = models.CharField(max_length=20)
bank_account_no      = models.CharField(max_length=15)
Done                 = models.BooleanField(default=False)
booking_date_time    = models.DateTimeField(auto_now_add=True, auto_now=False)
Entrance_date_time   = models.DateTimeField(auto_now_add=False, auto_now=True)# Must be modified to work with Entrance Date and Time

def __str__(self):
    return self.booking_id

def get_booking_id(self):
    bank_number = self.bank_number
    branch_number = self.branch_number
    id = # logic for calculating boking ID from bank_number, branch_number and other fields accessible from self.<field_name>
    return id

def save(self, *args, **kwargs):
    self.booking_id = self.get_booking_id()
    super(Customer, self).save(*args, **kwargs)
Ashwin Bande
  • 2,693
  • 2
  • 10
  • 22