Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you give a Django app a verbose name for use throughout the admin?

Tags:

python

django

People also ask

What does verbose name mean in Django?

verbose_name is a human-readable name for the field. If the verbose name isn't given, Django will automatically create it using the field's attribute name, converting underscores to spaces. This attribute in general changes the field name in admin interface.

How do I change app label in Django admin?

Rename the folder which is in your project root. Change any references to your app in their dependencies, i.e. the app's views, the urls.py and settings.py files. Edit the database table django_content_type with the following command: UPDATE django_content_type SET app_label='' WHERE app_label=''

What we can do in admin portal in Django?

Overview. The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.


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 appropriate AppConfig subclass to be configured explicitly in INSTALLED_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'

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'

If you have more than one model in the app just create a model with the Meta information and create subclasses of that class for all your models.

class MyAppModel(models.Model):
    class Meta:
        app_label = 'My App Label'
        abstract = True

class Category(MyAppModel):
     name = models.CharField(max_length=50)

Well I started an app called todo and have now decided I want it to be named Tasks. The problem is that I already have data within my table so my work around was as follows. Placed into the models.py:

    class Meta:
       app_label = 'Tasks'
       db_table = 'mytodo_todo'

Hope it helps.


Give them a verbose_name property.

Don't get your hopes up. You will also need to copy the index view from django.contrib.admin.sites into your own ProjectAdminSite view and include it in your own custom admin instance:

class ProjectAdminSite(AdminSite):
    def index(self, request, extra_context=None):
        copied stuff here...

admin.site = ProjectAdminSite()

then tweak the copied view so that it uses your verbose_name property as the label for the app.

I did it by adding something a bit like this to the copied view:

        try:
            app_name = model_admin.verbose_name
        except AttributeError:
            app_name = app_label

While you are tweaking the index view why not add an 'order' property too.


First you need to create a apps.py file like this on your appfolder:

# appName/apps.py

# -*- coding: utf-8 -*-             
from django.apps import AppConfig

class AppNameConfig(AppConfig):
    name = 'appName'
    verbose_name = "app Custom Name"

To load this AppConfig subclass by default:

# appName/__init__.py
default_app_config = 'appName.apps.AppNameConfig'

Is the best way to do. tested on Django 1.7

My custom App Name

For the person who had problems with the Spanish

This code enable the utf-8 compatibility on python2 scripts

# -*- coding: utf-8 -*-