Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form is not valid... WHY?

the model:

class Operation(models.Model):
  operation_type = models.ForeignKey(OperationType)
  category = models.ForeignKey(Category, default=0)
  related_account = models.ForeignKey(Account, related_name = 'related_account', null = True)
  comments = models.TextField(null = True)

the code:

def detail(request, category_id):
  class OperationCategoryOnlyForm(forms.ModelForm):
    class Meta:
      model = Operation
      fields = ('operation_type', 'category', 'related_account', )

  from django.forms.models import modelformset_factory
  OperationFormSet = modelformset_factory(Operation, form=OperationCategoryOnlyForm)
  if request.method == "POST":
    formset = OperationFormSet(request.POST, queryset=Operation.objects.filter(category=category_id))
    if formset.is_valid():
      formset.save()
# HERE IS THE FORMSET, WHICH OVERLAPS THE POSTED FORMSET - this was intentionaly to get the current result not result before save, but when you want to debug - you should pay attention at such things.    
#  formset = OperationFormSet(queryset=Operation.objects.filter(category=category_id))

  c = {"formset" : formset,}
  c.update(csrf(request))
  return render_to_response("reports/operation_list.html", c)

the template: UPDATED:

<form method="post" action="">
  {% csrf_token %}
  {{ formset.management_form }}
  {{ formset.errors }}
  {{ formset.non_field_errors }}
  {{ formset.non_form_errors }}
<table>
 {% for form in formset.forms %}
tr><td> {{ form.errors }} </td><td> {{ form.non_field_errors }}</td></tr>
<tr><td>{{ form.id }}</td><td>{{ form.instance.comments }}</td><td>{{ form.operation_type }}<br>{{ form.related_account }}</td><td>{{ form.category }}</td></tr>
  {% endfor %}
</table>
<input type="submit" value="submit">
</form>

I found that form.is_valid() = false - but i have no idea how to get the reason WHY...

UPD - I updated the template according to comment - nothing is result....

(this is very stupid rule, that i have to write less code than question - code is self explaining and is the essence of question - Almost always the question reduces into one sentence but there's no way to reduce code)

like image 755
Tomasz Brzezina Avatar asked May 20 '12 10:05

Tomasz Brzezina


1 Answers

Yeah, because you're creating another, empty formset before displaying it. Add print formset.errors before or directly after if checking for is_valid().

like image 73
ilvar Avatar answered Sep 24 '22 06:09

ilvar