Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set initial value to modelform in class based generic views

I'm using Class based generic views, can anybody suggest me how can i set the initial values to update form?

I tried using get_initial() method but didn't got any success. Following is the code which i tried

  class IncidentUpdateView(UpdateView):       form_class = IncidentForm       form_class.initial = {"badge_number": '88888'}       model = Incident       template_name = 'hse/incident/incident_update.html'       def get_initial(self, form_class):         initials = {          "badge_number": '88888'          }         form = form_class(initial=initials)        return form       def get_success_url(self):         return reverse_lazy('hse-incident', args=[self.object.id]) 
like image 690
Asif Avatar asked Jun 19 '12 07:06

Asif


1 Answers

You should define a get_initial method which returns a dictionary that contains the initial values:

class IncidentUpdateView(UpdateView):      def get_initial(self):         return { 'value1': 'foo', 'value2': 'bar' } 

Alternatively, you can define an initial value:

class IncidentUpdateView(UpdateView):     initial = { 'value1': 'foo', 'value2': 'bar' } 
like image 77
Simeon Visser Avatar answered Sep 21 '22 02:09

Simeon Visser