Menu
  • HOME
  • TAGS

How to save Django ModelFormSet?

python,django,modelform,formset

As I said in the comment, it doesn't make sense to pass a queryset of Employees to the instantiation of the Salary formset. I think given you're trying to keep these two models completely separate for whatever reason, and you want to create new Salary instances each time, you would...

Error in modelformset_factory : matching query does not exist

django,formset

This exception occurs because of some errors in the referential integrity of my DB. I had to clean it up first in order to have the modelformset_factory working. Thanks to PeterDeGlopper ! After that, the rendering slowness of the modelform_factory came from an over complex unicode model method....

How to Check for Unique Data in Django Form?

python,django,forms,django-forms,formset

I think I fixed it by adding an 'if' statement to check for None. if (firstname or lastname) is not None: firstnames.append (firstname) lastnames.append (lastname) ...

ManagementForm Data is Missing When Using Formset Prefix

python,django,django-forms,django-views,formset

The problem is in method cars rendering to a response.html page and displaying the rendered form at url http://..../vehicle/cars instead of vehicle/trucks. The "Management Form Error" was raised because "POST" happened the second times while still at url vehicle/cars form and not in vehicle/trucks form. Updated 5 hinted the problem....

Using a FormSet in a ClassBasedView in django

python,django,django-forms,django-class-based-views,formset

If you will take a look on sources of Django views and check how FormView is working, you find that it just overrides default get and post methods of base View class and adds some additional methods for the form handling. So you can: try to assign your formset to...

Model Fomset and deletion

django,modelform,formset

You can iterate over the formset.deleted_forms: for form in formset.deleted_forms: if form.instance.pk: form.instance.delete() As alternative you can check for DELETE key in the cleaned_data: for job_form in formset: if job_form.is_valid(): if job_form.cleaned_data.get('DELETE') and job_form.instance.pk: job_form.instance.delete() else: instance = job_form.save(commit=False) instance.account = account instance.save() ...

django Formset won't save

django,formset

I suspect that the formset is invalid but instead of displaying of the formset with errors you returning the redirect. You should move the redirecting to one level right, into the if statement: if formset.is_valid(): ... return HttpResponseRedirect(reverse("Pipettes.views.month", args=(year, month))) UPDATE: If your formset is not validated but you don't...

django saving model with formset

django,formset

The problem seems to be that you are returning the response from inside the for loop. Try this instead: account = PatientAccount.objects.get(pk=account_id) payments = Payments.objects.filter(account=account) total_cost = 0 for hasjob in account.hasjobs.all().select_related('job'): total_cost += hasjob.quantity * hasjob.job.cost*(1.0 - hasjob.discount / 100.0) PaymentFormset = modelformset_factory(Payments, form=PaymentModelForm, can_delete=False) formset = PaymentFormset(request.POST or...

Complex multi-model object creation with CreateView in Django

django,django-forms,formset,inline-formset

I've solved this problem using the technique described here rather than using a formset: https://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/...

Django Forms Manual Formatting

django,forms,formset

Here is one solution with a bit of hacking for the titles of the forms. if you would like to get rid of that I would recommend using python's zip function in your view to combine the formset and a list of the titles. an example of doing that cand...

How Can I display a form or formset based on the submissions of a Post - Django?

django,formset

Actually i didn't fully understand your code (why used while, why you getting item.quantity from db, quantity musn't be in POST data?), but your code can be something like; if request.method =='POST': session = request.session._session_key formtype = request.POST.getlist('name') formsets = [] for f in formtype: if f == 'eggs': item...

Django REST framework: save related models in ModelViewSet

django,django-forms,django-rest-framework,formset

I was dealing with similiar issue this week and I found out, that django rest framework 3 actually supports nested writable serialisation (http://www.django-rest-framework.org/topics/3.0-announcement/#serializers in subchapter Writable nested serialization.) Im not sure if nested serialisers are writable be default, so I declared them: ingredients = RecipeIngredientSerializer(many=True, read_only=False) steps = RecipeStepSerializer(many=True, read_only=False)...

Why is My Django Form Executed Twice?

python,django,forms,formset

The form2 view is run twice, once as a redirect from form1 which creates the form and renders the template, missing the if request.method == 'POST' part as this time around the request is a 'GET'. When you submit form2 back to the same view method it prints the line...