在admin字段django-cms中添加我自己的类

时间:2017-01-26 18:35:38

标签: django django-cms

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

在我的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)

任何想法都是我遗漏了一些东西,之后,我再次对组件进行迁移。

提前致谢!

1 个答案:

答案 0 :(得分:1)

您可以使用form的{​​{1}}属性为管理员指定自定义表单。

因此,使用下面链接的文档中的示例,看起来像;

ModelAdmin

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

所以在你的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 中你需要类似的东西;

admin.py