#modelformset_factory, BaseModelFormSet Error on Submission

8 messages · Page 1 of 1 (latest)

robust birch
#

I am using modelformset_factory to create forms. When the forms are completed and submitted in the template patients_doses_form.html
I get " Dose formset is not valid" in line 1077 of the patient_doses_view in views.py and the following error is displaying in the terminal:
Dose formset errors: [{'dose_id': ['This field is required.']}, {'dose_id': ['This field is required.']}, {}]
Form errors: <ul class="errorlist"><li>dose_id<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
Form errors: <ul class="errorlist"><li>dose_id<ul class="errorlist"><li>This field is required.</li></ul></li></ul> class
dose_id is the primary key of the model dose class at line 44 of models.py.
The DoseForm class and BaseDoseFormSet class are in line 286-322 of forms,py.

How do I fix this error? Thanks Marshall

The code is on github at https://github.com/marshallapierce/pkinecticdrugdosing

GitHub

Contribute to marshallapierce/pkinecticdrugdosing development by creating an account on GitHub.

mortal sundial
#

your DoseForm does not appear to include dose_id ?

#

You should really look into splitting your views, forms etc up into modules with mutiple files in.

robust birch
#

I am not following your recommendation as views, models, forms, templates and util are modules. I know it doesn't have dose_id in it but dose_id is a primary key in the database that is autoincremented. I tried adding it yesterday and I got the same error. Maybe I added it incorrectly if it is needed.

robust birch
#

I was able to get the modelformset to work .The code used is in form.py which includes doseform, basedoseformset, and doseformset. The primary key, dose_id, had to added to the dose form. The dose_id settings where finicky and took some time to get set properly. It must be a hidden field, and required = false. The patient_doses_view is working and the debugging code was causing errors that took a lot of iterations to fix. The patient_doses_form.html required a hidden field to be added for the hidden dose_id field. The formset will display empty fields if no doses have been added to the database. As the form(s) are completed and submitted they are redisplayed with all previously input data and one empty form to be completed. The formset does not crash if a empty form is submitted. There is one thing I need to fix or delete as it are not acting as expected, but the basic functionally is working correctly. I was using Copilot to help a lot but it got stuck in a box and would not come out. I then used X's AI Grok which functions much better and prompts you with debugging code to add to help clarity where errors are occurring. Grok also gives ideas for improvements without being prompted. Marshall

modest harbor
#

I think formsets are a mistake in the modern era 🙂

robust birch
#

depends on what you are doing i would imagine.

modest harbor
#

did you like grok for coding? I normally just stick with claude 3.5 in Windsurf (vscode clone w/ ai)