Django for loop overwrites saved modelform

…衆ロ難τιáo~ 提交于 2019-12-13 01:55:35

问题


The following code saves to database but the values inputted to the form2 overwrites values inputted to Form1 in database. However the Assumptions.Name is not overwritten and has both values Form1 and Form2. Also, if I refresh the page form is expanding and have more rows with previously saved values in it. How to avoid and save Form1 and Form2 data to database correctly?

views.py

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

model_names = ['Form1', 'Form2']


def get_assumptions(request):

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

    if request.method == 'POST':

        print('Reached post')

        pdb.set_trace()

        for thing in model_names:

            formset = AssumptionsFormset(request.POST)

            if formset.is_valid():



                if thing in request.POST:

                    print('template name in model_names')

                    for form in formset:

                        print('in for loop' +thing)

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

    else:

        formset = AssumptionsFormset()

        print('reached else')

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

models.py

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

class Assumptions(models.Model):

    Worst_Case = models.FloatField(null=True, blank=True, default=None)
    Grey_Case = models.FloatField(null=True, blank=True, default=None)
    Red_Case = models.FloatField(null=True, blank=True, default=None)
    Blue_Case = models.FloatField(null=True, blank=True, default=None)
    Green_Case = models.FloatField(null=True, blank=True, default=None)
    Best_Case = models.FloatField(null=True, blank=True, default=None)
    Name = models.TextField(null=True, blank=True, default=None)

assumptions.html

<div class="form">
<form action="" method="post">
{% csrf_token %}
{{ formset.management_form }}
{{ formset.non_form_errors.as_ul }}
{% for vardas in model_names %}
<h1>{{vardas}}</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="hidden" name="{{vardas}}" />
{% endfor %}
<input type="submit" value="Next">

</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_Case', 'Grey_Case', 'Red_Case', 'Blue_Case', 'Green_Case', 'Best_Case']
        exclude = ['Name']

来源:https://stackoverflow.com/questions/51218297/django-for-loop-overwrites-saved-modelform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!