There are a couple of issues here. Firstly, self.method will not work. In the context of the class body, there is no self where you declare PositiveIntegerField .
Secondly, the transfer of the called call will not be performed because the called gets the binding at compile time and does not change at run time. So if you define, say
class OrderDocumentBase(PdfPrintable): create_number = lambda: return 0 number = models.PositiveIntegerField(default=create_number) class Invoice(OrderDocumentBase): create_number = lambda: return 1
All instances of Invoice will still get 0 as the default value.
One way I can solve this is to override the save() method. You can check if number was not, and set it to default before saving.
class OrderDocumentBase(PdfPrintable): number = models.PositiveIntegerField() def save(self, *args, **kwargs): if not self.number: self.number = self.DEFAULT super(OrderDocumentBase, self).save(*args, **kwargs) class Invoice(OrderDocumentBase): DEFAULT = 2 class CreditAdvice(OrderDocumentBase): DEFAULT = 3
I checked above with a slight change (made OrderDocumentBase abstract since I did not have PdfPrintable ) and it worked as expected.
source share