0

i've created a page which has a formset, i want to check all forms if they have error, then call save method, but create() automatically call save method! is there away to prevent it please ? here is my views.py

def addNewGuestPopup(request):
    if request.method == "POST":
        form_no = int(request.POST.get('form_no'))
        removed_form = request.POST.get('removed_form').split(",")
        error_form_exits = []
        new_guests = []
        for i in range(form_no):
            if str(i) not in removed_form:
                full_name = request.POST.get('fullname-'+str(i))
                dob = request.POST.get('dob-'+str(i)).split("-")
            try:
                visitor = Vistor.objects.get(full_name=full_name, dob=datetime.datetime(int(dob[0]), int(dob[1]), int(dob[2])))
                error_form_exits.append(i)
            except Vistor.MultipleObjectsReturned:
                pass
            except Vistor.DoesNotExist:
                visitor = Vistor.objects.create(full_name=full_name, 
                                                dob=datetime.datetime(int(dob[0]), int(dob[1]), int(dob[2])),
                                                admin=request.user)
                new_guests.append(visitor)

        # return JsonResponse({'error_form':error_form})
        print(len(error_form_exits))

        return JsonResponse({'data':list(Vistor.objects.values_list('full_name', flat=True)),
                          'error_form':error_form_exits}, safe=True)
return render(request, 'main/forms.html')

i dont want to use django formset is there a way to prevent create from save until all forms been checked please? note : dob and full_name are unique together thank you ..

artiest
  • 554
  • 5
  • 20
  • It looks that you are "mimicking" a `FormSet`: it might be worth to work with a `FormSet`, since this removes a lot of boilerplate code: https://docs.djangoproject.com/en/4.0/topics/forms/formsets/ – Willem Van Onsem Jan 23 '22 at 14:47
  • @WillemVanOnsem thank you for your reply, as i mention i dont want to use django formset, i cant control it as i want, if you have an idea please let me know – artiest Jan 23 '22 at 15:03
  • 1
    well exactly what do you want to control that you can't with a `FormSet`? – Willem Van Onsem Jan 23 '22 at 15:07
  • @WillemVanOnsem i dont know how to send all forms to ajax request, to post without reloading – artiest Jan 23 '22 at 15:24
  • 1
    if it is wrapped in a `
    `, the click event invokes an `$.ajax(...)` with as `data: $("#bla").serialize(),`, this will encode all data accordingly.
    – Willem Van Onsem Jan 23 '22 at 15:35
  • @WillemVanOnsem one more issues: i have to kind of forms : main form , with formset to add guests, in the same url, i've created to views, but it wont show data for formsets when i use `{{form.full_name}}` – artiest Jan 23 '22 at 15:42
  • @WillemVanOnsem i tried this way before (using django formset),but not worked https://stackoverflow.com/questions/70806379/name-in-request-post-always-returns-false-django-python?noredirect=1#comment125175610_70806379 – artiest Jan 23 '22 at 15:44
  • @WillemVanOnsem i have to use two views form in the same url, i think django formset doesnt work will in this case, the second view should be submitted via js help – artiest Jan 23 '22 at 15:47
  • @WillemVanOnsem please is it possible via django formset ? – artiest Jan 23 '22 at 16:00
  • @WillemVanOnsem please can you help me to achieve that – artiest Jan 23 '22 at 17:33

0 Answers0