[Answered ]-Django set default value of a model field to a self attribute

2👍

You can’t do this using the default argument. The best bet is to override the save method:

def save(self, *args, **kwargs):
    if not self.id and not self.printable:
        self.printable = self.link_fattura()
    return super(Fattura, self).save(*args, **kwargs)

0👍

Sorry I read you question wrong, that’s not possible without javascript because your model hasn’t been saved at that stage yet.

You could forecast the url by doing something like:

def link_facttura(self):
    if not self.id:
        return some_url/fattura/%d/ % Fattura.objects.latest().id+1
    return u""

But it’s ugly and likely to cause erros once you start deleting records

Leave a comment