Reputation: 116
I was trying to customize my user but at the same time keep it as simple as possible, I wanted the username field to display as "nickname" but still keep all the behaviour and properties of the username. What I do when I am extending a django class is the ctrl+click thing on vscode to go inspect the super class (I was extending the AbstractUser this time) and here is the code for the username field in django's AbstractUser:
username = models.CharField(
_('username'),
max_length=150,
unique=True,
help_text=_('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'),
validators=[username_validator],
error_messages={
'unique': _("A user with that username already exists."),
},
)
Usually, I'd just paste the field in my custom class and override the _('username')
with _('nickname')
but that doesn't feel like the right thing to do here as there's a lot going on in that field, plus I'd have to also import the username_validator to keep that behaviour.
Now my question is; how do I override the verbose_name field (I guess that is what it's called) and still keep other behaviours.
Upvotes: 0
Views: 709
Reputation: 3076
Basing on this article, as you said it yourself, you have to explicitly specify verbose_name
while creating the model:
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40, verbose_name='Lasty')
Tried it, seems okay (at least on edit form, but I guess effect is the same for list view):
Considering User
model is kinda special, I guess you could simply subclass AbstractUser
class like this:
class MyUser(AbstractUser):
username = models.CharField(max_length=40, unique=True, verbose_name="Nickname")
Considering we're subclassing full-blown user, as per this paragraph, I believe we won't have to do anything else (register custom managers etc.).
Upvotes: 1