Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Django CreateView to handle formset - It doesnt validate
    primarykey
    data
    text
    <h2>The problem</h2> <p>I'm trying to modify the class-based view 'CreateView' to handle a formset instead of a form.</p> <p>When client does a GET request, the formset is displayed to the client correctly. The problem is when the client submit the form with a POST.</p> <p>When Django recieve POST, it lands in form_invalid() and the form.errors say 'this field is required' for the length and name field.</p> <pre><code>class Service(models.Model): TIME_CHOICES = ( (15, '15 minutes'), (30, '30 minutes'), ) length = models.FloatField(choices=TIME_CHOICES,max_length=6) name = models.CharField(max_length=40) class ServiceForm(ModelForm): class Meta: model = Service ServiceFormSet = modelformset_factory(Service,form=ServiceForm) class ServiceEditView(CreateView): template_name = "service_formset.html" model = Service form_class = ServiceForm success_url = 'works/' def form_valid(self, form): context = self.get_context_data() formset = context['formset'] if formset.is_valid(): self.object = form.save() return HttpResponseRedirect('works/') else: return HttpResponseRedirect('doesnt-work/') def form_invalid(self, form): print form.errors return HttpResponseRedirect('doesnt-work/') def get_context_data(self, **kwargs): context = super(ServiceEditView, self).get_context_data(**kwargs) if self.request.POST: context['formset'] = ServiceFormSet(self.request.POST) else: context['formset'] = ServiceFormSet(queryset=Service.objects.filter(user__exact=self.request.user.id)) return context </code></pre> <h1>My question is</h1> <p>How can I use a createview to handle a formset? What am I missing to get it do validate correctly?</p> <p>The tutorial I've taken most of the bits from so far <a href="http://haineault.com/blog/155/" rel="nofollow">http://haineault.com/blog/155/</a></p> <h2>In short, what I've done so far</h2> <p>Since the form.errors variable say each field is required, I think it expects a regular form not a formset -> I'm missing some option that tell the CreateView it's a formset.</p> <p>I've also tried the solution suggested here: <a href="http://www.kevinbrolly.com/" rel="nofollow">http://www.kevinbrolly.com/</a>.</p> <pre><code>class BaseServiceFormSet(BaseModelFormSet): def __init__(self, *args, **kwargs): super(BaseServiceFormSet, self).__init__(*args, **kwargs) for form in self.forms: form.empty_permitted = False </code></pre> <p>But it didnt make any difference.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload