Assigning initial field values ​​in Django related admin forms

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 now() class Meta : model = ConstantDef widgets = { ... } 

or something like that ...

 class ConstantDefAdminForm(ModelForm) : class Meta : model = ConstantDef widgets = { ... } def __init__(self, ...) : # some initialisation of a_date_field super(ConstantDefAdminForm, self).__init__(...) 

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

+4
source share
5 answers

Here is an approach that might work. In the admin model class, change the value of obj.a_date_field before the form is bound. The default value for the date field must be a new value.

 class MyModelAdmin(ModelAdmin): ... def get_object(self, request, object_id): obj = super(MyModelAdmin, self).get_object(request, object_id) if obj is not None: obj.a_date_field = datetime.now() return obj 

Note that get_object not documented, so this is a bit hacked.

+4
source

In Django 1.4, declaring default=<callable> in a model works well:

 class MyModel(models.Model): dt = models.TimeField(null=True, blank=True, default=datetime.datetime.now) 

Each time you add an entry, the default value for the field is updated.

But using the default field parameter causes some problems with the log history of DateField objects, which are each time recorded as changed as well when they are not changed. So I made a decision based on fooobar.com/questions/1385917 / ... :

 import datetime class MyModelAdminForm(forms.ModelForm): class Meta: model = MyModel def __init__(self, *args, **kwargs): super(MyModelAdminForm, self).__init__(*args, **kwargs) self.fields['dt'].initial = datetime.datetime.now class MyModelAdmin(admin.ModelAdmin): form = MyModelAdminForm fields = ('dt',) 
+4
source

I had a similar problem and I found a solution from here

I think you want to do this:

 class yourAdminModel(admin.ModelAdmin): fields = ['your_date_field'] def add_view(self, request, form_url="", extra_context=None): data = request.GET.copy() data['your_date_field'] = datetime.date.today() # or whatever u need request.GET = data return super(yourAdminModel, self).add_view(request, form_url="", extra_context=extra_context) 
+1
source

You should be able to use auto_now with your DateTime field, which according to the docs will automatically set the value to now () every time the form is saved

0
source

Since Django 1.7 there is a get_changeform_initial_data function in ModelAdmin that sets the initial values ​​of the form:

 def get_changeform_initial_data(self, request): return {'dt': datetime.now()} 
0
source

Source: https://habr.com/ru/post/1385915/


All Articles