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()
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 in accordance with the comment - nothing worked ....
(this is a very silly rule that I should write less code than the question - the code itself explains and is the essence of the question - almost always the question comes down to one sentence, but there is no way to reduce the code)
source share