I would like to generate an "update formset" to update a large number of instances in one go, where the updated field is empty in the database (at the time of rendering the update formset) but is "suggested" using an already filled-in field.
My model goes here:
class Event(models.Model):
plan_start_time = models.TimeField()
actual_start_time = models.TimeField(blank=True, null=True)
So what I'd like is a table where actual_start_time
is suggesting plan_start_time
as pre-filled (initial) value.
What I tried:
class EventForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(EventForm, self).__init__(*args, **kwargs)
self.fields['actual_start_time'].initial = self.fields['plan_start_time'] # doesn't work
self.fields['actual_start_time'].initial = "08:00" # doesn't work either
self.fields['actual_start_time'].initial = datetime.time(hour=8) # doesn't work...
self.initial['actual_start_time'] = self.fields['plan_start_time'] # this puts '<django.forms.fields.TimeField object at 0x067BA330>' in the field... not its value
class Meta:
model = Event
fields = ('plan_start_time', 'actual_start_time', )
# Specifying form to use
MyFormSet = modelformset_factory(Event, extra=0, fields=('plan_start_time', 'actual_start_time'), form=EventForm)
I guess the question comes down to: can I access a form field value in the __init__()
of the form?
If not, what would be a workaround for this issue?
As an alternative, I'm looking into rendering plan_start_time
as a hidden field and then copying it over to the actual_start_time
fields for every field in my table, using Javascript. But I'd be surprised if there would be no Django client side way to do this, so would like to avoid JS for now.
Using Django 1.8.