简体   繁体   中英

Django inline formset validation passes but no objects are saved

I have this form:

class ServiceTargetForm(forms.ModelForm):
    class Meta:
        model = ServiceTarget
        fields=('target_type', 'target_value', 'target_threshold')

all the fields are required.

To make everything more complicated this model has unique_together condition:

class ServiceTarget(models.Model):
    service = models.ForeignKey(Service, verbose_name = _(u"Service"))
    all the other fields
    ...
    class Meta:
        ordering = ['service__car__name', 'service__car__reg_no','service__name',]
        verbose_name = _(u"Service target")
        verbose_name_plural = _(u"Service targets")
        unique_together = ("service__id", "target_type")

so saving empty form like this:

ServiceTargetFormSet = inlineformset_factory(Service, ServiceTarget, extra = 1, form = ServiceTargetForm, formset = MyInlineFormSet)
if request.method == 'POST':
    form = ServiceForm(request.POST, cars_queryset = cars_queryset, client = account)

    if form.is_valid():
        new_service = form.save(commit = False)
        stformset = ServiceTargetFormSet(request.POST, instance = new_service)
        if stformset.is_valid():
            form.save()
            stformset.save()
            messages.success(request, ugettext(u"New service was successfully created."))
            redirect_to = '/services/'
            return HttpResponseRedirect(request, redirect_to)
        else:
            messages.error(request, ugettext(u"Creating new service failed"))

Should defenately fail - yet does not. Queryset that gets posted is :

<QueryDict: {u'servicetarget_set-0-target_value': [u''], u'servicetarget_set-INITIAL_FORMS': [u'0'], u'name': [u'asdasd2q31'], u'recipients': [u'4'], u'car': [u'22'], u'message_delivery_method': [u'email'], u'servicetarget_set-0-target_threshold': [u''], u'servicetarget_set-TOTAL_FORMS': [u'2'], u'more-rows-count': [u'0'], u'servicetarget_set-0-target_type': [u'km'], u'servicetarget_set-1-target_type': [u'km'], u'csrfmiddlewaretoken': [u'blablabla'], u'servicetarget_set-1-target_value': [u''], u'servicetarget_set-MAX_NUM_FORMS': [u''], u'message': [u'asdasdasd'], u'servicetarget_set-1-target_threshold': [u'']}>

and it comes out clean, even though all the important fields are empty : [u'']. So one would think that if it validates - it also saves succesfully... But it does not. No records can be found in database. other than the records created by form - which validates and saves just fine.

Update: found similar question which is also unanswered and it seems to be exactly the same problem: Make inlineformset in django required

I created testscript for myself to use in shell and it brings up all the same results:

q = 'servicetarget_set-0-target_value=&servicetarget_set-INITIAL_FORMS=0&servicetarget_set-0-id=&name=asdasdasd&recipients=4&car=13&servicetarget_set-1-target_type=km&servicetarget_set-1-service=&servicetarget_set-0-target_threshold=&servicetarget_set-TOTAL_FORMS=2&servicetarget_set-0-service=&servicetarget_set-1-target_threshold=&more-rows-count=0&servicetarget_set-0-target_type=km&servicetarget_set-1-id=&csrfmiddlewaretoken=90018471df61d327b6288cb38ad15630&servicetarget_set-1-target_value=&servicetarget_set-MAX_NUM_FORMS=&message=asdasdasd&message_delivery_method=email'

from django.http import QueryDict
qd = QueryDict(q)
from account.models import Account
account = Account.objects.get(id = 2)
from services.models import Service, ServiceTarget
from services.forms import ServiceTargetForm, MyInlineFormSet, ServiceForm
from django.contrib.auth.models import User
u = User.objects.get(id = 5)
from guardian.shortcuts import get_objects_for_user
cars_queryset = get_objects_for_user(u, 'cars.can_access_car').order_by('reg_no').filter(client = account)
form = ServiceForm(qd, cars_queryset = cars_queryset, client = account)
form.is_valid()
True
new_service = form.save(commit = False)
from django.forms.models import inlineformset_factory
ServiceTargetFormSet = inlineformset_factory(Service, ServiceTarget, fk_name="service", extra = 1, form = ServiceTargetForm, formset = MyInlineFormSet)
stformset = ServiceTargetFormSet(qd, instance = new_service)
stformset.is_valid()
True
stformset.cleaned_data
[{}, {}]
stformset.errors
[{}, {}]
stformset.non_form_errors()
[]

Update2: Django formsets: make first required? accepted answere here says why the errors are not raised - apparently form has to have empty_permited set to False.

Its not exactly a duplicate of this:

Django formsets: make first required?

But the answer there applies here too.

Empty forms pass validation if formsets form class does not have empty_permitted parameter set to False.

So you can fix this in one of two ways:

  1. the way they do it in the link I added
  2. doing same thing in formsets form class __init__ method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM