Package: django-param-field
A Django model field that uses a DSL to define, generate, and validate,
custom forms.ParamFieldallows you to store something like this:width:Decimal->max:50.0min:5.0height:Decimal->max:40.0min:3.0painted:Bool->default:Falseinscription:Text->max_length:30and generate the django equivalent form as needed:fromdjangoimportformsclassCustomForm(forms.Form):width=forms.DecimalField(max_value=50,min=5)height=forms.DecimalField(max_valur=40,min=3)painted=forms.BooleanField()inscription=forms.CharField(max_length=30)This is useful for creating user defined forms, or custom per models
forms.RequirementIt has been tested onPython 3Django 1.9, 1.10InstallationFrom the repository$gitclonehttps://github.com/secnot/django-param-field$pythonsetup.pyinstallor from pypi$pipintalldjango-param-fieldUsageAdd param_field to INSTALLED_APPS# settings.pyINSTALLED_APPS=[...'param_field',]Add the field to your model:# models.pyfromdjang.dbimportmodelsfromparam_fieldimportParamField,ParamDictclassCustomProduct(models.Model):name=models.CharField(max_length=44)...params=ParamField(blank=True,max_length=3000)Now that you have a working model to create a new instance with its
parameters write:params="""
    width: Dimmension-> max:50.0 min:5.0
    height: Dimmension-> max:40.0 min:3.0"""CustomProduct.objects.create(name='Custom wooden box",params=params)And the FormView that generates the forms from the model# views.pyfromdjango.shortcutsimportrender,get_object_or_404fromdjango.views.genericimportFormViewfromdjangoimportformsfrom.modelsimportCustomProductclassCustomProductFormView(FormView):template_name='product_form.html'form_class=forms.Formdefdispatch(self,request,*args,**kwargs):"""Find requested CustomProduct it's needed both in post and get
        requests so the form can be genereted"""pk=self.kwargs['pk']self.product=get_object_or_404(CustomProduct,pk=pk)returnsuper().dispatch(request,*args,**kwargs)defget_context_data(self,**kwargs):"""Send product info to template"""context=super().get_context_data(**kwargs)context['product']=self.productreturncontextdefget_form(self,form_class=None):"""Generate form form param_field"""# NOTE: params.form(...) will return None when it doesn't# containt any field.returnself.product.params.form(**self.get_form_kwargs())defform_valid(self,form):"""Do what ever you want with the form, at this point it's a
        validated django form like any other"""custom_parameters=form.cleaned_data...Read thisblog
postfor a longer
tutorial that includes an example on how to handle File and Image
fields.SyntaxEach ParamField can have one or more fields with the following syntaxfieldname:type->property:valuefieldname- A lowercase name starting with a letter and followed
by letters, numbers, and/or underscores. The default max name length
is 30 characters.type- One of the supported field types (All starting with
uppercase)BoolDecimalDimmensionIntegerTextTextAreaFileImageproperty- One or more of the properties supported by the field
type followed by a value.ALL: hidden. required, label, help_textBool: defaultInteger: default, even, odd, max, min, choicesDecimal: default, max, min, choices, max_digits, max_decimalsText: default, max_length, min_length, choicesTextArea: default, max_lengthFile: (doesn’t support hidden)Image: (doesn’t support hidden)value- One of the value types supported by the property to its
leftBoolean - True/FalseDecimal - 1.33, 6.44Integer - 44String - “string with scape "chars" “Value list - [value, value, value]ConfigurationThe absolute limits for the fields properties are configurable throughsettings.py, for examplePARAM_INT_MAXcontrols the max
allowed value for integermaxproperty, so creating a new Integer
field wheremaxis bigger will fail.These are the available options with their default value:# settings.py# Max lengths for label and help_text stringsPARAM_LABEL_MAX_LENGTH=40PARAM_HELP_TEXT_MAX_LENGTH=200PARAM_NAME_MAX_LENGTH=30# Max and Min integer values, these have been chosen so integers don't cause# problems when stored in any DBPARAM_INT_MAX=2147483647PARAM_INT_MIN=-2147483648# The maximum number of digits allowed and the max decimal placesPARAM_DECIMAL_MAX_DIGITS=20PARAM_DECIMAL_MAX_DECIMALS=4# Decimal max and min (must have valid number of digits/decimals)PARAM_DECIMAL_MAX=Decimal("9999999999999999.9999")PARAM_DECIMAL_MIN=Decimal("-9999999999999999.9999")# Dimmension digits/decimalsPARAM_DIMMENSION_MAX_DIGITS=12PARAM_DIMMENSION_MAX_DECIMALS=4# Dimmension max and minPARAM_DIMMENSION_MAX=Decimal("99999999.9999")PARAM_DIMMENSION_MIN=Decimal("0.0")# Text/TextArea max lengthPARAM_TEXT_MAX_LENGTH=300# max_length used by ParamField when it isn't suppliedPARAM_FIELD_MAX_LENGTH=3000TestingOnce the app has been added to settings.py, you can run the tests with:$pythonmanage.pytestparam_fieldReferencesDomain speficific languages python
slideSmall django-param-field
tutorialwith a
longer example than the one in this README.TODOBetter parser error messages