lflorespy
lflorespy

Reputation: 27

How to change name by default of permission in Django?

Help me to change the name of permissions of my app

The permissions of my app:

[] articles | article | Can add article
[] articles | article | Can change article
[] articles | article | Can delete article

I want to have the name of the permissions in spanish

Articulo - Puede cambiar articulo
Articulo - Puede eliminar artículo
Articulo - Puede crear articulo

EDIT:

I have two opctions:

1. Create Middleware AdminLocaleURLMiddleware

2. Overwrite unicode method of Permission:

from django.utils import six
def permissions_new_unicode(self):
    nombre_clase = six.text_type(self.content_type)
    nombre_permiso = six.text_type(self.name)
    if 'Can delete' in nombre_permiso:
        nombre_permiso = nombre_permiso.replace('Can delete', 'Puede eliminar')
    elif 'Can add' in nombre_permiso:
        nombre_permiso = nombre_permiso.replace('Can add', 'Puede crear')
    elif 'Can change' in nombre_permiso:
        nombre_permiso = nombre_permiso.replace('Can change', 'Puede modificar')

    return u'%s - %s' % ( nombre_clase.title(), nombre_permiso)

Replace the __unicode__ method in Permission Class

from django.contrib.auth.models import Permission
Permission.__unicode__ = permissions_new_unicode

Upvotes: 2

Views: 2358

Answers (2)

mh-firouzjah
mh-firouzjah

Reputation: 844

there is short but tricky answer. I wouldn't recommend to do this way neither reject that. but the solution is: edit the app_labeled_name function decorated as property of this path: path/to/django/contrib/contenttypes/models.py of ContentType class, to become like this:

@property
def app_labeled_name(self):
    model = self.model_class()
    if not model:
        return self.model
    return '%s | %s' % (apps.get_app_config(model._meta.app_label).verbose_name,
                        model._meta.verbose_name)

the trick is to use apps.get_app_config(model._meta.app_label).verbose_name instead of model._meta.app_label, so it would use the same verbose_name as whatever use set for your app in the AppConfig subclass of your app.

Upvotes: 0

dhana
dhana

Reputation: 6525

If you want to explicitly set language for django admin section use this

from django.conf import settings
from django.utils import translation


    class AdminLocaleURLMiddleware:

        def process_request(self, request):
            if request.path.startswith('/admin'):
                request.LANG = getattr(settings, 'ADMIN_LANGUAGE_CODE', settings.LANGUAGE_CODE)
                translation.activate(request.LANG)
                request.LANGUAGE_CODE = request.LANG

Then put somewhere in settings.py:

ADMIN_LANGUAGE_CODE='es'

and add middleware:

MIDDLEWARE_CLASSES = (
   ...
    'utils.multilang.middleware.AdminLocaleURLMiddleware',
   ....

Upvotes: 1

Related Questions