I am about to start a large Django project at work. And the key features are form handling. There is going to be a lot of forms that the users of the app is going to use. And one requirement is that it should be possible to edit the forms in the admin interface of the application.
That is, it is not a requirement to add/delete form fields. But it should be possible to edit form field attributes. E.g change which fields which is required and such.
Anybody have any good solutions on how to do this. I’m thinking that I might have to subclass the fields I want to be dynamic and do something there. I guess that all the models fields have to have "blank=True/null=True" and some how add some meta information in a separate model(?) about the model which declares which fields are required. And then use that information when forms is displayed and validated.
Before I start off doing this I would really like some input in how to design such a solution, anybody got ideas on how it should be done?
After more research, I’ve learned that you could do a lot with factory functions that would return the form with the given attributes set. So it looks like the problem is not that hard do solve.
I would make a function that returns a form with the right attributes/fields set. But the part that I haven’t found a good solution for is how to manage this in the admin interface. I am thinking that I would make a db.Model that stores information about another models fields. Where I can set which is required and such.
And then in the function that returns the form, go trough that model and return a form with the right attributes. But how to make that model (which should mirror another model’s fields) in a good way?
You should use certain models for this. For every form that might be customised this way, a new database entry must be created. I guess, it should look like this:
In FormSettings.form you should store some address of form, like say . and when form is built (in init), it should look for an entry in db and use attribs, that were described for it.
You can make it create db entries easily, if you use own metaclass for your forms and make it register in the database (create proper FormSettings entry), when a class is created. It will be done once when the process is started, which shouldn’t be that bad.
I hope it helps a little. If you have further questions, I’ll be happy to help 🙂 I like those non standard appliances of django 🙂 This is where all the fun begins 🙂
EDIT:
OK, let’s say, that you want to you have app food and form FavouriteFoodForm with food_name field. You store in the database in formsettings table as food.FavouriteFoodForm and add one attribute setting: field=’food_name’, attrib_name=’required’, attribute_value=’True’ (it’s not perfect, but still it’s not that bad’).
No in FavouriteFoddForm you look for the settings in database, so you do:
and you iterate over settings
and here you simple call exec and set proper attribute:
This should set attributes to required=True.