I have a very long forms.py and I’d like to split it to smaller parts with as few as possible changes in the code.
Any ideas?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The main reason that we put Django form classes into a
forms.pyfile is convention. It makes it easy for others to look at your application and know what kinds of things he or she will find in any given file. It’s good to follow conventions, but it’s not the law and you’re free to organize your code as you please.Here’s what I’d do:
First of all, see if you can reduce the amount of code by factoring out duplicated code. Common validation logic could be pulled into custom validators. Forms that share fields or functionality could inherit from a common parent.
If you have created custom field classes, you could separate all those into a file called something like
formfields.pyin the application directory, and simply import them intoforms.py. Likewise, if you have custom validators, you can take those out and import them.If none of that reduces filesize enough for you, I would split up my form classes by function into several files, e.g.
forms_user.py,forms_products.py,forms_data_entry.pyor whatever. That way, it’s still obvious what we will find in a given file. You will have to change import statements wherever your old forms are referenced, but those should be the only changes necessary.