I am having trouble understanding this, and come up with a way to link to self inside the default keyword of the model field:
Here is what I have:
class Bank(models.Model): number = models.CharField(max_length=10) class Account(models.Model): bank = models.ForeignKey(Bank, related_name="accounts") number = models.CharField(max_length=20) created = models.DateTimeField(auto_now_add=True) creator = models.ForeignKey(User)
So, I'm trying to access myself inside the class definition, which does not seem to work because python does not know where I am, since it is not an object yet.
I tried different things like:
special_code = models.CharField(max_length=30, default='%s-%s' % (number, bank.number))
But in this case, it does not recognize bank.number , because the bank has only a property with models.ForeignKey .
I also tried using the method inside the Account class:
def bank_number(self): return self.bank.number
and then:
special_code = models.CharField(max_length=30, default='%s-%s' % (number, bank_number()))
It was a little stupid because it still needs itself. Is there a way I can do this?
I need it to store the number inside the database, so using this method will not help:
def special_number(self): return '%s-%s' % (self.number, self.bank.number)