Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would we allow users to create their own fields for forms in Django?

this might be a slightly broad question, but in essence, I'd like to allow my site's users to be able to add fields (text or char fields) to forms so that other users can fill them out. How exactly should I structure my models/forms/views to allow for this type of customization? Thanks!

like image 658
Aurora Avatar asked Nov 10 '22 15:11

Aurora


1 Answers

Using metaclass:

from django.forms import BaseForm

fields = {
    'name': forms.CharField(max_length=50),
    'id': forms.IntegerField()
}

type('CustomForm', (BaseForm,), { 'base_fields': fields })

For a more comprehensive approach, use django-forms-builder:

http://django-forms-builder.readthedocs.org/en/latest/

Mezzanine also supports this:

http://mezzanine.jupo.org/docs/packages.html#module-mezzanine.forms

like image 143
tuxcanfly Avatar answered Nov 15 '22 07:11

tuxcanfly