Dynamically changing form fields in view ommits those fields from cleaned_data

Hello there. I’m working on a report page on a django project. There is a general reports page, where all reports are listed with their forms, and the user fills the form of their report of choice and presses a button to go to that particular reports page to see results and/or change the form inputs. I’m creating one of those reports.

I want 2 specific fields to appear only in the specific report page, and not in the general page. The way I do it is, I declare those fields as HiddenInput in forms.py, and manually change them into ModelChoiceField in the form_valid function of the view. Everything works fine, except those 2 fields are not contained in the cleaned_data dictionary of the form (I need to look at data to find them). Any ideas why/how to tackle this? Thanks in advance.

Rather than changing the form field type, I’d set the css for the field to “display: none;” and set the disabled attribute on the form field.

Thank you for your quick reply! I tried this but it did not solve my problem, unfortunately. The thing is, those two ModelChoiceFields have thousands of entries, which slows down the general page (where there are other reports too, so it adds up) With the CSS solution, those are still calculated.

However I just found a solution for anyone facing a similar problem: I did override the get_form function of the view to return the form with the 2 attributes’ widgets changed to HiddenInput when the view has no request attribute (on the general page there’s no request to that view yet, since the user hasn’t requested a specific report yet), and the form as it normally is when there is a request attribute (meaning the user did request that specific report, thus accessing the page). It works like a charm.