def setup_field self cls field_name field cls _meta get_field field_na

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def setup_field(self, cls, field_name):
field = cls._meta.get_field(field_name).formfield()
for key in field.__dict__:
setattr(self, key, field.__dict__[key])
class UsernameField(forms.CharField):
def __init__(self, *args, **kwargs):
super(UsernameField, self).__init__(*args, **kwargs)
setup_field(self, User, 'username')
self.label = 'Логин на сайте'
self.help_text = 'Вы можете использовать символы: a-z, 0-9 и дефис.'