简体   繁体   中英

Django modelformset_factory UnboundLocalError

I am getting error message the UnboundLocalError local variable 'Assumptions' referenced before assignment (after line else:). I wonder how is that even possible given that Assumptions is a model and is imported to the views.py. Any advise how to solve it would be highly appreciated. Also, I am trying to create multiple forms based on Assumptions modelform and save them into the database model Assumptions. Please advise if this code is the correct design pattern. Thank you.

views.py

from django.shortcuts import render
from .forms import  modelformset_factory, AssumptionsForm
from .models import Assumptions


def get_assumptions(request):

    if request.method == 'POST':

        if 'name' in request.POST:      

            formset = modelformset_factory(Assumptions, form = AssumptionsForm, extra = 5)

            if formset.is_valid():

                print('valid form')            

                for form in formset:

                    print('Looping forms')

                    Assumptions =  form.save(commit='False')
                    Assumptions.Name = 'name'
                    Assumptions.save()

    else:

        formset = modelformset_factory(Assumptions, form = AssumptionsForm, extra = 5)

    return render(request, 'assumptions.html', {'formset': formset})

assumptions.html

<div class="form">
<form action="" method="post">
{% csrf_token %}
{{ formset.management_form }}
{{ formset.non_form_errors.as_ul }}
{% for name in ['A', 'B'] %}
<h1>var</h1>
<table id="formset" class="form">
{% for form in formset.forms %}
  {% if forloop.first %}
  <thead><tr>
    {% for field in form.visible_fields %}
    <th>{{ field.label|capfirst }}</th>
    {% endfor %}
  </tr></thead>
  {% endif %}
  <tr class="{% cycle 'row1' 'row2' %}">
  {% for field in form.visible_fields %}
    <td>
    {# Include the hidden fields in the form #}
    {% if forloop.first %}
      {% for hidden in form.hidden_fields %}
      {{ hidden }}
      {% endfor %}
    {% endif %}
      {{ field.errors.as_ul }}
      {{ field }}
    </td>
  {% endfor %}
  </tr>
{% endfor %}
</table>

<input type="submit", name='name', value="save" />
{% endfor %}



</form>
</div>

forms.py

from django import forms
from django.forms import modelformset_factory, ModelForm
from .models import Assumptions

class AssumptionsForm(ModelForm):

    class Meta:
        model = Assumptions
        fields = ['Worst', 'Base', 'Best']
        exclude = ['Name']

models.py

from django.db import models
from django.forms import ModelForm

class Assumptions(models.Model):

    Worst = models.FloatField(null=True, blank=True, default=None)
    Base = models.FloatField(null=True, blank=True, default=None)
    Best = models.FloatField(null=True, blank=True, default=None)
    Name = models.TextField(null=True, blank=True, default=None)

You are shadowing the imported Assumptions name by assigning a new value to it here:

Assumptions =  form.save(commit='False')

You should use a different name for your model instance. The usual practice is to use a lowercase name, ie

assumptions = form.save(commit='False')
assumptions.Name = 'name'
assumptions.save()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM