Mojtaba Yousefi
Mojtaba Yousefi

Reputation: 676

Django Delete Button Action for ModelFormset

I've got a modelformset which is shown in below:

views.py

def manage_authors(request):
    AuthorFormSet = modelformset_factory(Author, fields=('name', 'title', 'birth_date'))
    if request.method == 'POST':
        if "del_btn" in request.POST:
            query = Author.objects.get(...).delete()
            formset = AuthorFormSet(request.POST, request.FILES)
            if formset.is_valid():
                formset.save()
                formset = AuthorFormSet(queryset=Author.objects.all())
                print "yes"
        else:
            formset = AuthorFormSet(request.POST, request.FILES)
            if formset.is_valid():
                formset.save()
                formset = AuthorFormSet(queryset=Author.objects.all())
    else:
        formset = AuthorFormSet(queryset=Author.objects.all())
    return render(request, "manage_authors.html", {"formset": AuthorFormSet, })

manage_authors.html

<form method="post" action="/manage_authors.html">{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
    {{ form.id }}
    <ul>
        {{ form.name }} {{ form.title }} {{ form.birth_date }}
        <input type="submit" name="del_btn" value="Delete"/>
    </ul>
{% endfor %}
<input type='submit' name="edit_btn" value='Edit / Add'/>

enter image description here

What query I can define so that the Delete button could work?

Now, I got problem with knowing which row must be deleted.

Thanks in advance

Upvotes: 2

Views: 4292

Answers (1)

Yugandhar Chaudhari
Yugandhar Chaudhari

Reputation: 3964

First I recommend registering your user model in admin.py:

admin.site.register(Author)

Django will handle the rest.

But if you want to do it with this code do like this:

<form method="post" action="/manage_authors.html">{% csrf_token %}
{{ formset.management_form }}
 for form in formset %}
    {{ form.id }}
    <ul>
        {{ form.name }} {{ form.title }} {{ form.birth_date }}
    <input type="submit" name="del_btn{{ form.instance.id }}" value="Delete"/>
</ul>
{% endfor %}
<input type='submit' name="edit_btn" value='Edit / Add'/>

So that primary key of object will relate to the delete button name.

Now, in views.py:

import re, urllib

def manage_authors(request):
    AuthorFormSet = modelformset_factory(Author, fields=('name', 'title', 'birth_date'))
    if request.method == 'POST':
        enurl=urllib.urlencode(request.POST)  # To convert POST into a string
        matchobj=re.search(r'del_btn\d', enurl)  # To match for e.g. del_btn1
        btnname=matchobj.group()  # Contains matched button name
        pri_key=btname[-1]  # slice the number of btn to identify primarykey 
        if matchobj:
            query = Author.objects.get(pk=pri_key).delete()
            formset = AuthorFormSet(request.POST, request.FILES)
            if formset.is_valid():
                formset.save()
                formset = AuthorFormSet(queryset=Author.objects.all())
                print "yes"
        else:
            formset = AuthorFormSet(request.POST, request.FILES)
            if formset.is_valid():
                formset.save()
                formset = AuthorFormSet(queryset=Author.objects.all())
    else:
        formset = AuthorFormSet(queryset=Author.objects.all())

    return render(request, "manage_authors.html", {"formset": AuthorFormSet, })

Upvotes: 3

Related Questions