Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

python - 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])
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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' }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...