Django - Can't remove empty_label from TypedChoiceField -
Django - Can't remove empty_label from TypedChoiceField -
i have field in model:
types_choices = ( (0, _(u'worker')), (1, _(u'owner')), ) worker_type = models.positivesmallintegerfield(max_length=2, choices=types_choices)
when utilize in modelform has "---------" empty value. it's typedchoicefield hasn't empty_label attribute., can't override in form init method.
is there way remove "---------"?
that method doesn't work too:
def __init__(self, *args, **kwargs): super(jobopinionform, self).__init__(*args, **kwargs) if self.fields['worker_type'].choices[0][0] == '': del self.fields['worker_type'].choices[0]
edit:
i managed create work in way:
def __init__(self, *args, **kwargs): super(jobopinionform, self).__init__(*args, **kwargs) if self.fields['worker_type'].choices[0][0] == '': worker_choices = self.fields['worker_type'].choices del worker_choices[0] self.fields['worker_type'].choices = worker_choices
the empty alternative model field choices determined within .formfield()
method of model field class. if @ django source code method, line looks this:
include_blank = self.blank or not (self.has_default() or 'initial' in kwargs)
so, cleanest way avoid empty alternative set default on model's field:
worker_type = models.positivesmallintegerfield(max_length=2, choices=types_choices, default=types_choices[0][0])
otherwise, you're left manually hacking .choices
attribute of form field in form's __init__
method.
django django-forms
Comments
Post a Comment