#665 closed enhancement (fixed)
Field.default should accept callables
| Reported by: | Adrian Holovaty | Owned by: | Adrian Holovaty | 
|---|---|---|---|
| Component: | Metasystem | Version: | |
| Severity: | minor | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | no | UI/UX: | no | 
Description
Field.default should accept callables, which would be evaluated at the time that the default value was set.
Example:
default=datetime.datetime.now
Change History (3)
comment:1 by , 20 years ago
comment:3 by , 19 years ago
| Resolution: | → fixed | 
|---|---|
| Status: | new → closed | 
This was fixed at some point a while ago.
  Note:
 See   TracTickets
 for help on using tickets.
    
how about allowing full callbacks that get passed in a self reference? That would make from your sample:
default=lambda self: datetime.datetime.now()
but would allow cool things like:
default=lambda self: self.otherfield.lower()
Sure, those could be done by _pre_save, too, but defaults kick in before validators, while _pre_save kicks in afer validators, so you often have to set blank=True to allow _pre_save to set the default on new records.