Dynamic Form Requirements in Django

I am going to launch a large Django project at work. Key features are form processing. There will be many forms that users of the application will use. And one of the requirements is that it should be possible to edit forms in the application admin interface.

That is, it is not necessary to add / remove form fields. But it should be possible to edit the attributes of the form field. For example, change the fields that are required, and such.

Anyone have any good decisions on how to do this. I think that I may have to subclass the fields that I want to be dynamic and do something there. I think that all model fields should have "blank = True / null = True", and some how to add some meta-information in a separate model (?) About a model that announces which fields are required. And then use this information when forms are displayed and validated.

Before I start doing this, I would really like to contribute to the development of such a solution, did anyone have any ideas on how to do this?


After further research, I found out that you can make a'lot with factory functions that return a form with a given set of attributes. So the problem does not seem to be that hard to solve.

I would make a function that returns a form with the desired attributes / fields. But the part that I did not find a good solution for is how to manage this in the admin interface. I think I will make db.Model which stores information about other model fields. Where can I establish what is required and the like.

And then in the function that returns the form, go through this model and return the form with the correct attributes. But how to make this model (which should reflect other fields of models) in a good way?

+3
source share
2 answers

. , , . , :

class FormSettings(Model):
  form = CharField(..)

class FormAttrib(Model):
  form_settings = ForeignKey(FormSettings)
  field = CharField(..)
  attrib_name=CharField(..)
  attrib_value=CharField(..)

FormSettings.form , , . ( init), db , .

db, ( FormSettings), . , , .

, . , :-) django:-) : -)

EDIT:

, , , - FavouriteFoodForm food_name. formsettings food.FavouriteFoodForm : field = 'food_name', attrib_name = 'required', attribute_value = 'True' ( , ).

FavouriteFoddForm , :

settings = FormSettings.objects.get(form=app_name + self.__class__.name)

for setting in settings.formattrib_set():

exec :

exec("getattr(self, settings.field)[attrib_name] = %s" % attrib_value)

= True.

+2

, - , , , . , .

, BuiltForm BuiltFormField:

class BuiltForm(models.Model): 
    name = models.CharField(max_length=32) 
    def form(self, data=None, initial=None):
        form = BuiltFormGenericForm(data, initial=initial)
        form.addBuiltFormFields(BuiltFormField.objects.filter(builtform=self, disabled=0))
        return form            

class BuiltFormField(models.Model):
    builtform = models.ForeignKey(BuiltForm)
    type = models.CharField(max_length=32, choices=ALL_FIELD_TYPES)
    label = models.CharField(max_length=32)
    fieldname = models.CharField(max_length=32)
    helptext = models.CharField(max_length=256, blank=True)
    required = models.BooleanField(default=False)
    sort_order = models.IntegerField(default=0)
    disabled = models.BooleanField(default=False)
    options = models.TextField(blank=True)
    def field(self):
        field = ALL_FIELD_MAPS.get(self.type)
        ## Build out the field, supplying choices, `required`, etc.

, . ALL_FIELD_TYPES - , . dict() , (CharField, EmailField, ChoiceField ..) . options pickle d ChoiceField. .

Form, BuiltForm. :

class BuiltFormGenericForm(forms.Form):
    built_form_fields = {}
    builtform = None
    def addBuiltFormFields(self, fields):
        for field in fields:
            self.fields[field.label] = field.field()
            self.built_form_fields[field.pk] = field
    def is_valid(self):
        # Do validation here.  My code for this is pretty big because of custom fields
        # and calculations that I have to squeeze in.

BuiltFormField , JavaScript, , , , BuiltFormField .

, .

+2

Source: https://habr.com/ru/post/1732034/


All Articles