It would be highly appreciated if you could advise how the template should be modified to scale model forms as per the code below. There are around 30 item models which are the same as parent model. Also, those model forms have to be saved to database separately so I assume there should be the same amount of model forms as there are models. Thank you very much in advance and looking forward to your insights.
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 = ['Bad', 'Likely', 'Best']
exclude = ()
models.py
from django.db import models
from django.forms import ModelForm
class Assumptions(models.Model):
Bad = models.FloatField(null=True, blank=True, default=None)
Likely = models.FloatField(null=True, blank=True, default=None)
Best = models.FloatField(null=True, blank=True, default=None)
class Meta:
abstract = True
class Item1(Assumptions):
pass
class Item2(Assumptions):
pass
class ItemN(Assumptions):
pass
views.py
from django.shortcuts import render
from .forms import modelformset_factory, AssumptionsForm
from .models import Item1, Item2, Assumptions
model_names = [item1, item2 ... itemN]
def get_assumptions(request):
for name in model_names:
AssumptionsFormSet = modelformset_factory(name, form = AssumptionsForm,
extra = 5)
if request.method == 'POST':
formset = AssumptionsFormSet(request.POST, prefix = str(name))
if formset.is_valid():
print('valid form')
for form in formset:
print('in for loop after valid form1')
name = form.save()
else:
formset = AssumptionsFormSet
print('reached else')
return render(request, 'assumptions.html', {'formset': formset})
assumptions.html
<div class="form">
{%for name in model_names%}
<form action="" method="post">
<h1>{{name}}</h1>
{% csrf_token %}
<h1>{{ name }}</h1>
{{ formset.management_form }}
{{ formset.non_form_errors.as_ul }}
<table id="formset1" 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.prefix}}, value={{name.prefix}} />
</form>
{% endfor%}
</div>
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/29b981d5-5f38-4039-977b-a1fd32e301c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment