django - Way to not duplicate codes between "models.py" and "forms.py"? -
i'm writing custom user model field.
while doing this, realized duplicating codes between "models.py" , "forms.py"?? 
for example:
models.py
class myuser(abstractbaseuser):     email = models.emailfield(         verbose_name='email address',         max_length=255,         unique=true,         db_index=true,     )      full_name = forms.charfield(         max_length=64,     )     username_field = 'email'     required_fields = ['full_name']  ... 
 forms.py
class registrationform(forms.form):     error_css_class = 'error'     required_css_class = 'required'       email = forms.emailfield(         label=_("email"),     )     full_name = forms.charfield(         label=_("full name"),     )     password1 = forms.charfield(         widget=forms.passwordinput,         label=_("password"),     )     password2 = forms.charfield(         widget=forms.passwordinput,         label=_("password (again)"),     ) ... here, find myself defining fields twice, don't know if if necessary, , if not, how can combine 2 fields in 1 line :(
any idea?
thanks.
a modelform can have fields model-bound , unbound. can override label attribute model field without having re-define field in form:
class registrationform(forms.modelform):     class meta:         model = myuser      error_css_class = 'error'     required_css_class = 'required'       def __init__(self, *args, **kwargs):         super(registrationform, self).__init__(*args, **kwargs)         self.fields['full_name'].label = _("full name")      password2 = forms.charfield(         widget=forms.passwordinput,         label=_("password (again)"),     ) although, don't see you're providing labels of model fields differently in form. can add label attribute @ model level too:
class myuser(abstractbaseuser):     email = models.emailfield(u'email address', max_length=255, unique=true,         db_index=true)     full_name = forms.charfield(u'full name', max_length=64)      username_field = 'email'     required_fields = ['full_name'] 
Comments
Post a Comment