Can you give a Django app a verbose name for use throughout the admin?
Django 1.8+
Per the 1.8 docs (and current docs),
New applications should avoid
default_app_config
. Instead they should require the dotted path to the appropriateAppConfig
subclass to be configured explicitly inINSTALLED_APPS
.
Example:
INSTALLED_APPS = [
# ...snip...
'yourapp.apps.YourAppConfig',
]
Then alter your AppConfig
as listed below.
Django 1.7
As stated by rhunwicks' comment to OP, this is now possible out of the box since Django 1.7
Taken from the docs:
# in yourapp/apps.py
from django.apps import AppConfig
class YourAppConfig(AppConfig):
name = 'yourapp'
verbose_name = 'Fancy Title'
then set the default_app_config
variable to YourAppConfig
# in yourapp/__init__.py
default_app_config = 'yourapp.apps.YourAppConfig'
Prior to Django 1.7
You can give your application a custom name by defining app_label in your model definition. But as django builds the admin page it will hash models by their app_label, so if you want them to appear in one application, you have to define this name in all models of your application.
class MyModel(models.Model):
pass
class Meta:
app_label = 'My APP name'
verbose name app in django admin interface
Since django 1.7 app_label does not work. You have to follow https://docs.djangoproject.com/en/1.7/ref/applications/#for-application-authors instructions.
That is:
- Create
apps.py
in your app's directory Write verbose name :
# myapp/apps.py
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = 'myapp'
verbose_name = "Rock ’n’ roll"In the project's settings change
INSTALLED_APPS
:INSTALLED_APPS = [
'myapp.apps.MyAppConfig',
# ...
]Or, you can leave
myapp.apps
inINSTALLED_APPS
and make your application load this AppConfig subclass by default as follows:# myapp/__init__.py
default_app_config = 'myapp.apps.MyAppConfig'
alles :)
How do I change the colum name of model in Django Admin?
You can use verbose_name
in your model itself without affecting your database or any other function. Like this column_name = models.CharField(verbose_name="name you desire")
and that will change the name of your column in django admin for you.
In your case you use the following code:
class contactEnquiries(model.Models):
cn_name = models.CharField(verbose_name="Name", your other config)
#your all other columns here and you can user verbose_name in all your model columns.
You can read the documentation here:
https://docs.djangoproject.com/en/4.1/ref/models/options/#verbose-name
Is it possible to rename the application's group name in Django?
You can do that in the verbose_name
[Django-doc] of the AppConfig
:
# app_name/apps.py
from django.apps import AppConfig
from django.utils.translation import gettext_lazy as _
class MyAppConfig(AppConfig):
verbose_name = _('My verbose name for the app')
In the __init__.py
of the app, you then specify this as the default AppConfig
for that app:
# app_name/__init__.py
default_app_config = 'app_name.apps.MyAppConfig'
How to set verbose_name for external app model in Django?
You almost got it right. For your changes on the proxy model to take effect you need to unregister the model from admin site first and then register the proxy model.
The example below is for social_django.Association
model.
# admin.py
from django.contrib import admin
from social_django.admin import AssociationOption
from social_django.models import Association
class AssociationProxy(Association):
class Meta:
proxy = True
verbose_name = 'custom model'
app_label = 'social_django'
admin.site.unregister(Association)
admin.site.register(AssociationProxy, AssociationOption)
This assumes you are using the default admin site
# urls.py
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
How to display multi-word app names properly (i.e. with spaces) in Django admin?
unfortunately, afik, django doesn't really support the notion of "verbose names" for apps. The admin just does app_name.title()
. So to get want you want, some more trickery is needed. Haven't tried (never needed this) but this solution may just work.
Django always capitalizes a model's verbose name in admin index page
It's not that easy...
- Make a copy of the
admin/index.html
template to yourtemplate/admin/index.html
- Create your own template filter:
lowerfirst_if_starts_with_v
in your
owntemplatetags/my_special_thing.py
directory
@register.filter(is_safe=True)
@stringfilter
def lowerfirst_if_starts_with_v(value):
"""Lowercase the first character of the value."""
return value and value[0] =='v' and value[0].lower() + value[1:]
- Load it in the
index.html
{%load my_special_thing%}
- Apply it to
index.html
on line23
<th scope="row"><a href="{{ model.admin_url }}"> \
{{ model.name|lowerfirst_if_starts_with_v }}</a></th>
And done.
Styling HTML for model property with verbose name in Django admin
You are missing the @property
decorator
checkout this answer
and the documentation
Related Topics
Python - Activate Conda Env Through Shell Script
Python Variables as Keys to Dict
Saving Upload in Flask Only Saves to Project Root
Enable Python to Connect to MySQL via Ssh Tunnelling
How to Write String Literals in Python Without Having to Escape Them
How to Configure Atom to Run Python3 Scripts
Is It Pythonic to Import Inside Functions
Modular Multiplicative Inverse Function in Python
Re.Sub Replace with Matched Content
Using Print() (The Function Version) in Python2.X