Home > Software design >  Setting an initial value of an extra field on a ModelForm from an UpdateView
Setting an initial value of an extra field on a ModelForm from an UpdateView

Time:12-01

I have a form which is a modelform with two extra fields. In my model, it includes a PointField which is made up of an easting and northing (the X & Y components of the Point). On my modelform, I do not display the PointField and instead display two DecimalFields for the easting and northing:

class QuoteForm(forms.ModelForm):
  easting = forms.DecimalField()
  northing = forms.DecimalField()
  class Meta:
    model = Quote
    fields = ('desc', 'client', 'contact', 'amount', 'easting', 'northing', 'note')      

  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.fields['note'].required = True
    self.helper = FormHelper()
    self.helper.form_method = 'post'
    self.helper.add_input(Submit('submit', 'Save Quote')
    )

For my CreateView I include a form_valid that takes the easting and northing and turns them into a point and updates the PointField record.

For my UpdateView, I want to go backwards from the PointField and populate the Easting and Northing DecimalFields on my form.

I am stuck on how to "pass" this information to my form.

I tried a get_form in my UpdateView like this, but it fails:

class QuoteUpdateView(PermissionRequiredMixin, UpdateView):
  permission_required = 'jobs.update_quote'
  redirect_field_name = 'dashboard'
  model = Quote
  form_class = QuoteForm    
  template_name = "jobs/quote_form.html"
  template_name_suffix = '_form'
  
  def get_form(self, form_class=None):
    form = super().get_form(form_class)
    quote = self.object
    pt = quote.ptgeom
    pt.transform(2953)
    form.fields['easting'] = pt[0]
    form.fields['northing'] = pt[1]
    return form

  def form_valid(self, form):     
    quote = form.save(commit=False)
    quote.ptgeom = geos.Point((form.cleaned_data['easting'], form.cleaned_data['northing']), srid=2953)
    quote.save()
    return super().form_valid(form)

  def get_success_url(self):
    return reverse('quote_detail', kwargs={'pk' : self.object.id})

In my approach so far, I am using the same form for both the create and update view. It feels like I could create a different form and use something like get_context_data to find the correct record, then update the fields, but this doesn't feel like the correct approach?

Is there a way to set these values from the UpdateView?

CodePudding user response:

You can grab and alter the intial values of fields in your get_form function via get_initial, included in the updateview.

  def get_form(self, form_class=None):
    form = super().get_form(form_class)
    quote = self.object
    pt = quote.ptgeom
    pt.transform(2953)
    #set form initial values
    initial_values = self.get_initial() 
    initial_values['easting'] = pt[0]
    initial_values['northing'] = pt[1]
    form.initial = initial_values
    return form
  • Related