Django - 在管理员中更改应用程序名称

时间:2017-07-04 22:14:41

标签: django

我不知道如何在django管理面板中更改外部应用的名称。我可以更改康斯坦斯应用程序的详细名称('我的常规设置'):

class ConstanceConfig(AppConfig):
    name = 'constance'
    verbose_name = 'My constance settings'

我不知道如何更改" config"名称。任何的想法? enter image description here

我的models.py:

from datetime import datetime
from datetime import timedelta
from urllib.parse import urlparse
from django.db import models
from django.urls import reverse
from mainapp.widgets import MyTextField
from django.template.defaultfilters import slugify
from smart_selects.db_fields import ChainedForeignKey
from django.db.models import Q
from constance import config


class Category(models.Model):
    category_name = models.CharField(max_length=30, unique=True,
                                 verbose_name='Nazwa kategorii')
    slug = models.SlugField()
    image = models.ImageField(upload_to='category_images',
                          verbose_name="Obrazek kategorii",
                          blank=True)
    category_description = models.TextField(default='Opis',
                                        verbose_name="Opis kategorii")

    class Meta:
        verbose_name_plural = " Kategorie"

    def save(self, *args, **kwargs):
        self.slug = slugify(self.category_name)
        super(Category, self).save(*args, **kwargs)

    def admin_image_thumb(self):
        '''
        Miniaturka obrazka kategorii dla panelu admina
        '''
        if self.image:
            return('<img src="/media/%s" height="42" width="42" />') % 
(self.image)
        else:
            return('<img src="/media/category_images/default.ico" alt="" />')

    admin_image_thumb.short_description = 'Miniaturka'
    admin_image_thumb.allow_tags = True

    def image_thumb(self):
        '''
        Miniaturka obrazka kategorii
        '''
        if self.image:
            return('/media/%s') % (self.image)
        else:
            return('/media/category_images/default.ico')

    def get_active_sites(self):
        return Site.objects.filter(Q(category=self) | Q(category1=self), 
is_active=True)

    image_thumb.short_description = 'Miniaturka'
    image_thumb.allow_tags = True

    def __str__(self):
        return self.category_name


class SubCategory(models.Model):
    category = models.ForeignKey(
        'Category',
        related_name='subcategory',
        on_delete=models.CASCADE,
        blank=True,
        null=True,
        verbose_name='Kategoria nadrzędna'
    )
    name = models.CharField(max_length=30, verbose_name="Nazwa subkategorii")
    slug = models.SlugField()

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(SubCategory, self).save(*args, **kwargs)

    def get_active_sites(self):
        return Site.objects.filter(Q(subcategory=self) | Q(subcategory1=self), is_active=True)

    class Meta:
        verbose_name_plural = " Subkategorie"

    def __str__(self):
        return self.name


def get_deadline():
    return datetime.today() + timedelta(days=20)


class Site(models.Model):
    category = models.ForeignKey('Category')
    subcategory = ChainedForeignKey(
        'SubCategory',
         chained_field='category',
        chained_model_field='category',
        show_all=False,
        auto_choose=True,
        default=None)
    name = models.CharField(max_length=config.TITLE_LENGTH_MAX, 
verbose_name="Tytuł")
    slug = models.SlugField()
    description = models.TextField(max_length=config.DESCRIPTION_LENGTH_MAX, 
verbose_name="Opis")
    # importuje zmienione TextFields widgets.py
    keywords = MyTextField(max_length=config.KEYWORDS_LENGTH_MAX, 
verbose_name="Słowa kluczowe")
    date = models.DateTimeField(default=datetime.now)
    date_end = models.DateTimeField(null=True, blank=True, default=None)
    url = models.URLField()
    is_active = models.BooleanField(default=False, verbose_name='Aktywna')

    flagged = models.TextField(max_length=400, verbose_name="Flagowanie", 
null=True, blank=True, default='')
    flagged_true = models.BooleanField(default=False, verbose_name='Oflagowana')

    category1 = models.ForeignKey('Category', related_name='category', 
blank=True, null=True, default=None)
    subcategory1 = ChainedForeignKey(
         'SubCategory',
        chained_field='category1',
        chained_model_field='category',
        related_name='subcategory',
        show_all=False,
        auto_choose=True, blank=True, null=True)

    group = models.ForeignKey('Group', default='Podstawowy',
                          help_text="<div id='group'><ul><li>Możesz dodać 
wpis do 1 kategorii</b></li></ul></div>")

    email = models.EmailField(help_text='Podaj adres email')
    user = models.CharField(max_length=100)
    kod = models.CharField(max_length=20)

    def save(self, *args, **kwargs):
         self.slug = slugify(self.name)
         # if len(self.flagged.splitlines() > 3):
         #     self.flagged_true = True
        super(Site, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse('site', args=[str(self.category.slug),
                                 str(self.subcategory.slug), str(self.slug), 
str(self.id)])

    def get_thumb(self):
        host = urlparse(self.url).hostname
        if host.startswith('www.'):
            host = host[4:]
        thumb = 'http://free.pagepeeker.com/thumbs.php?size=l&url=http://' + host
        # thumb = 'https://api.thumbalizr.com/?url=http://' + host + '&width=500'
        return thumb

    class Meta:
        verbose_name_plural = "Strony"

    def __str__(self):
        return self.name


class Group(models.Model):
    group_name = models.CharField(max_length=30, verbose_name='Nazwa grupy', 
unique=True)
    codes = models.TextField(null=True, blank=True)
    pay = models.CharField(
        max_length=10,
        choices=(('PAID', 'PŁATNY'), ('FREE', 'DARMOWY')),
        default='FREE',
        verbose_name="Czy płatny")
    time = models.CharField(
        max_length=3,
        choices=(('T', 'TAK'), ('N', 'NIE')),
        default='N',
        help_text='Czy wpis ma być wyłączony po jakimś czasie czy 
bezterminowy')
    days = models.IntegerField(default=1)
    premium_box = models.CharField(
         max_length=3,
    choices=(('T', 'TAK'), ('N', 'NIE')),
    default='N',
    help_text='Czy wpis ma być wyświetlany w okienku reklamowym')
category = models.CharField(
    max_length=2,
    choices=(('1', '1'), ('2', '2')),
)

is_active = models.BooleanField(default=True, verbose_name='Aktywna')

class Meta:
    verbose_name_plural = "Grupy wpisów"

def __str__(self):
    return self.group_name

我的admin.py的一部分:

from constance.admin import ConstanceAdmin, ConstanceForm, Config


class CustomConfigForm(ConstanceForm):
    def __init__(self, *args, **kwargs):
        super(CustomConfigForm, self).__init__(*args, **kwargs)


class ConfigAdmin(ConstanceAdmin):
    change_list_form = CustomConfigForm
    change_list_template = 'admin/config/change_list.html'


admin.site.unregister([Config])
admin.site.register([Config], ConfigAdmin)

在我的venvs文件中,我找到了admin.py(在constance目录中)。有:

class Config(object):
    class Meta(object):
        app_label = 'constance'
        object_name = 'Config'
        model_name = module_name = 'config'
        verbose_name_plural = _('config')
        abstract = False
        swapped = False

        def get_ordered_objects(self):
            return False

        def get_change_permission(self):
            return 'change_%s' % self.model_name

        @property
        def app_config(self):
            return apps.get_app_config(self.app_label)

   _meta = Meta()

我可以改变

verbose_name_plural = _('config')

为:

verbose_name_plural = _('My config label')

我仍然无法从我的admin.py文件中更改此内容...

2 个答案:

答案 0 :(得分:1)

正确显示应用程序的名称,您指向模型的名称。做你想做的事的一种方法是修改模型的Meta:

class Config(models.Model):
    class Meta:
        verbose_name = 'Some other name'

答案 1 :(得分:0)

我找到了解决方案。两天的试验和错误......;)

{{1}}