Answer the question
In order to leave comments, you need to log in
How to remove from modelformset?
Good day.
Either I'm completely stupid or I don't know anymore.
The second day I suffer, I can not delete from the modelformset with the button on the site.
Actually a piece of the template
{{ formset.management_form }}
{% for form in formset %}
{{ form.id }}
{{ form.DELETE }}
<tr class="edt {% if forloop.first %} shadow {% endif %}"
{% if forloop.counter|divisibleby:"2" %}style="background-color: white"
{% else %} style="background-color: #F2F2F2"
{% endif %}>
<td> {{ form.title }} </td>
<td>{{ form.priceMazut }} </td>
<td>{{ form.priceSPBT }} </td>
<td>{{ form.priceDTl }} </td>
<td>{{ form.priceDTm }} </td>
<td>{{ form.priceDTz }} </td>
<td class="last"></td>
</tr>
{% endfor %}
def manage_articles(request):
if not request.user.is_staff:
return redirect('/')
ArticleFormSet = modelformset_factory(Article, fields=('title', 'priceMazut', 'priceSPBT',
'priceDTl', 'priceDTm', 'priceDTz'))
if request.method == 'POST':
formset = ArticleFormSet(request.POST, request.FILES)
if formset.is_valid():
formset.save()
return redirect('/?$')
else:
formset = ArticleFormSet()
return render(request, "table/manage_articles.html", {"formset": formset})
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = ('title', 'priceMazut', 'priceSPBT', 'priceDTl', 'priceDTm', 'priceDTz')
ArticleFormSet = modelformset_factory(Article, form=ArticleForm, can_delete=True)
formset = ArticleFormSet()
class ArticleFormSet(BaseModelFormSet):
def clean(self):
super(ArticleFormSet, self).clean()
for form in self.forms:
title = form.cleaned_data['title'].upper()
form.cleaned_data['title'] = title
form.instance.title = title
priceMazut = form.cleaned_data['priceMazut'].upper()
form.cleaned_data['priceMazut'] = priceMazut
form.instance.priceMazut = priceMazut
priceSPBT = form.cleaned_data['priceSPBT'].upper()
form.cleaned_data['priceSPBT'] = priceSPBT
form.instance.priceSPBT = priceSPBT
priceDTl = form.cleaned_data['priceDTl'].upper()
form.cleaned_data['priceDTl'] = priceDTl
form.instance.priceDTl = priceDTl
priceDTm = form.cleaned_data['priceDTm'].upper()
form.cleaned_data['priceDTm'] = priceDTm
form.instance.priceDTm = priceDTm
priceDTz = form.cleaned_data['priceDTz'].upper()
form.cleaned_data['priceDTz'] = priceDTz
form.instance.priceDTz = priceDTz
Answer the question
In order to leave comments, you need to log in
I figured out the deletion, but now when I add new rows or edit existing ones, it gives an error instead of saving
AttributeError at /table/edit/
'NoneType' object has no attribute 'group'
def manage_articles(request):
if not request.user.is_staff:
return redirect('/')
ArticleFormSet = modelformset_factory(Article, fields=('title', 'priceMazut', 'priceSPBT',
'priceDTl', 'priceDTm', 'priceDTz'))
if request.method == 'POST':
enurl = urllib.parse.urlencode(request.POST)
matchobj = re.search(r'del_btn\d', enurl)
pri_key = matchobj.group()[-1]
if matchobj:
query = Article.objects.get(pk=pri_key).delete()
formset = ArticleFormSet(request.POST, request.FILES)
if formset.is_valid():
formset.save()
formset = ArticleFormSet(queryset=Article.objects.all())
return redirect('/?$')
else:
formset = ArticleFormSet(request.POST, request.FILES)
if formset.is_valid():
formset.save()
return redirect('/?$')
else:
formset = ArticleFormSet()
return render(request, "table/manage_articles.html", {"formset": formset})
{{ formset.management_form }}
{% for form in formset %}
{{ form.id }}
{{ form.instance.id }}
{{ form.DELETE }}
<tr class="edt {% if forloop.first %} shadow {% endif %}"
{% if forloop.counter|divisibleby:"2" %}style="background-color: white"
{% else %} style="background-color: #F2F2F2"
{% endif %}>
<td> {{ form.title }} </td>
<td>{{ form.priceMazut }} </td>
<td>{{ form.priceSPBT }} </td>
<td>{{ form.priceDTl }} </td>
<td>{{ form.priceDTm }} </td>
<td>{{ form.priceDTz }} </td>
<td><input type="submit" name="del_btn{{ form.instance.id }}" value="Delete"/></td>
<td class="last"></td>
</tr>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question