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
解決
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)
他のヒント
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'