I have a fairly simple Django application (v1.3 on Red Hat) for which I use the admin application to create and modify database entries. One of the fields in my base model is a date field. Each time the corresponding field is displayed in a new or editable admin form, I would like the initial value of this field to be today the date (and time). Then the user can change it if he wants.
I know that I can set the default field value in my model definition (i.e. in models.py). This works great when a database record is first created. But for subsequent calls to the change form, the caller, which I assigned to the default parameter (datetime.datetime.now), does not explicitly call.
I looked - and tried - pretty well all of the many proposed solutions described elsewhere in stackoverflow, without success. Most of them seem to revolve around inserting initialization code into a subclass of ModelForm, for example. or something like this ...
class ConstantDefAdminForm(ModelForm) : a_date_field = DateField(initial="datetime.datetime.now")
or something like that ...
class ConstantDefAdminForm(ModelForm) : class Meta : model = ConstantDef widgets = { ... } def __init__(self, ...) :
But none of these approaches work. The initial value of the field is always set to the value that is stored in the database. My reading of the Django documentation is that the various ways of overlaying the initial field values ββin forms work only for unbound forms, not connected ones . Right?
But this feature (for selective redefinition of currently stored values) seems to be such a popular requirement that I am convinced that there must be a way to do this.
Has anyone there been able to do this?
Thanks in advance,
Phil
source share