Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - How can I set/change the verbose name of Djangos User-Model attributes?

I'm building a website which contains several languages.

Therefore I need to set verbose names of all attributes in all my model classes (with the internationalizaton-helper). Unfortunately I don't find a way to change the verbose names of the user attributes...

And overwriting every single form isn't really a nice way to do it, right?

Thanks for your help!

Ron

like image 973
Ron Avatar asked Oct 20 '25 09:10

Ron


2 Answers

It depends on what you need this for. Generally, verbose_name is only used within the context of the admin. If that's what you need to worry about, then you can create a proxy model and then make your user admin use that:

from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _

class CustomUser(User):
    class Meta:
        proxy = True
        app_label = 'auth'
        verbose_name = _('My Custom User')

Then, in admin.py:

 from django.contrib.auth.admin import UserAdmin
 from django.contrib.auth.models import User

 from .models import CustomUser

 admin.site.unregister(User)
 admin.site.register(CustomUser, UserAdmin)
like image 103
Chris Pratt Avatar answered Oct 22 '25 02:10

Chris Pratt


Since this is the first answer on google when searching for django verbose name I thought I'd correct Chris Pratt's answer from 2012 to be correct for django 2.0

If you only want to update the model name in the Admin panel you can simply add a Meta class to your user model. Note the slight change from Chris' answer.

class Meta:
    verbose_name = 'My Custom User'
like image 22
Emanuel Lindström Avatar answered Oct 22 '25 03:10

Emanuel Lindström



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!