Created
September 30, 2012 15:59
-
-
Save rfaga/3807256 to your computer and use it in GitHub Desktop.
Creating dynamic form on a Django TextField containing one or more columns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### on my forms.py, a function to generate this form | |
def make_revision_form(account): | |
revision_type = account.program.revision_type | |
hints = revision_type.get_hints() | |
fields = {'revision_field': forms.CharField(widget=forms.HiddenInput, required=False), | |
'account_id': forms.CharField(widget=forms.HiddenInput, required=False, initial=account.id)} | |
for i, hint in enumerate( hints ): | |
fields['grade_%d'%(len(hints)-i)] = forms.CharField( | |
label="Critério %s"%chr(i+65), help_text=hint, | |
required=True) | |
return type('RevisionForm', (forms.BaseForm,), { 'base_fields': fields }) | |
### my revision type model | |
class RevisionType(models.Model): | |
class Meta: | |
verbose_name = _(u"Peso de notas para revisões de inscrições") | |
name = models.TextField(verbose_name=_(u'Nome do método de pontuação')) | |
weights = models.TextField(verbose_name=_(u'Peso das notas de cada campo, separadas por ponto-vírgula')) | |
hints = models.TextField(verbose_name=_(u'Especificações sobre o que é cada campo')) | |
def get_hints(self): | |
return [ hint.strip() for hint in self.hints.split(";")] | |
def get_weights(self): | |
return [ Decimal(weight.strip()) for weight in self.weights.split(";")] | |
def __unicode__(self): | |
return self.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment