在管理字段 django-cms 添加我自己的 class

add my own class in admin field django-cms

大家好 Y 在 djando CMS 中创建我自己的应用程序,现在我想添加我自己的 class 和 id 到我的领域.. 你试试这个,但我没有获得任何成功的结果。

在我的 model.py 我有这个

class Entry(models.Model):
  TYPES_CHOICES = ( 
    ('none', 'not specified'),
    ('s', 'Series'),
    ('mb', 'Multiples Bar'),
    ('b', 'Bar suggestion'),
  )
  app_config = AppHookConfigField(HealthConfig)
  code = models.CharField(blank=True, default='', max_length=250)
  url_suggestion = models.CharField(blank=True, default='', max_length=250, verbose_name="URL for Suggestion" )


  health_placeholder = PlaceholderField('health_info')
  objects = AppHookConfigManager()

  def __unicode__(self):
    return self.url

  class Meta:
    verbose_name_plural = 'entries'

现在在我的 form.py 我有这个

from django import forms
from .models import Entry


class EntryForm(forms.ModelForm):

  class Meta:
    model = Entry
    fields = '__all__'


  def __init__(self, *args, **kwargs):
    super(EntryForm, self).__init__(*args, **kwargs)
    self.fields['code'].widget.attrs={ 
      'id': 'my_code', 
      'class': 'code_class', 
    }

我的admin.py终于变成这样了

from django.contrib import admin
from cms.admin.placeholderadmin import PlaceholderAdminMixin
from .cms_appconfig import HealthConfig
from .models import Entry
from .forms import EntryForm
from aldryn_apphooks_config.admin import ModelAppHookConfig, BaseAppHookConfig


class EntryAdmin(ModelAppHookConfig, PlaceholderAdminMixin,  admin.ModelAdmin):
  # pass

  fieldsets = (
    ('General data', {
      'fields':('app_config','chart', 'url',('count', 'code', 'start'))
    }),
    ('Suggestion',{
      'classes':('collapse', 'suggestion',),
      'fields':('url_suggestion',('key1_suggestion_name','key1_suggestion'),('key2_suggestion_name','key2_suggestion'), 'primary_suggestions')
    }),
  )

  list_display =('app_config' ,'url', 'chart');

  list_filter = (
    'app_config',
  )

  form = EntryForm

  class Media:
    js = ('health/js/admin/healthAdmin.js',)
    css = {
      'all': ('health/css/admin/admin_area.css',)
    }

admin.site.register(Entry, EntryAdmin)

我觉得我遗漏了什么,之后,我再次迁移了组件。

提前致谢!

您可以使用 ModelAdminform 属性为管理员指定自定义表单。

所以使用下面链接的文档中的示例,它看起来像;

from django import forms
from django.contrib import admin
from myapp.models import Person


class PersonForm(forms.ModelForm):
    class Meta:
        model = Person
        exclude = ['name']


class PersonAdmin(admin.ModelAdmin):
    exclude = ['age']
    form = PersonForm

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form

所以在您的 admin.py 中您需要类似的东西;

from .forms import EntryForm

class EntryAdmin(admin.ModelAdmin):
    form = EntryForm