formset has_changed always return true.

50 views
Skip to first unread message

Mei B

unread,
Jun 10, 2019, 10:34:21 PM6/10/19
to Django users
       if request.method == "POST":
            formset = forms.Formset(data=request.POST, initial=initial)
            if not formset.has_changed(): #has_changed always returns true! 
                msg = "No changes detected."

            if formset.has_changed() and formset.is_valid()
                do stuff...
        else:
            formset = forms.Formset(initial=initial)
            formset_get = formset

What am i doing wrong?

Gil Obradors

unread,
Jun 11, 2019, 11:59:46 AM6/11/19
to django...@googlegroups.com
Hi!
Have you instanced first de form?
Maybe this code from djano help you

from django.http import HttpResponseRedirect
from django.shortcuts import render

from .forms import NameForm

def get_name(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = NameForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:
            return HttpResponseRedirect('/thanks/')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = NameForm()

    return render(request, 'name.html', {'form': form})

Maybe you can check has_changed() after  form= Nameofyourforminformspy(request.POST) ?
good luck



Missatge de Mei B <lidokaw...@gmail.com> del dia dt., 11 de juny 2019 a les 4:33:
--
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...@googlegroups.com.
To post to this group, send email to django...@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/23af35da-2f85-4512-9842-6b6811f3988b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mei B

unread,
Jun 11, 2019, 7:23:47 PM6/11/19
to Django users
Yes I am already instantiated the form and check has_changed after.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Gil Obradors

unread,
Jun 12, 2019, 1:05:59 AM6/12/19
to django...@googlegroups.com
Can you paste codes?  Forms.py and views.py

El dc., 12 de juny 2019, 1:24, Mei B <lidokaw...@gmail.com> va escriure:
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages