无法在 Django Admin 中编辑 slug 字段
Can't edit slug field in Django Admin
我有一个 Django 应用程序,它的模型有一个 slug 字段。到目前为止,我只是简单地对模型的标签字段进行 slug 处理,并在管理员中将 slug 设置为只读。我的客户现在希望我允许他们自己将 slug 编辑成他们想要的任何内容。我取消了管理员中的只读访问权限,并更改了模型上的保存方法以不使用其 slugified 标签。然而,当我去管理员那里并尝试更改 slug 时,当我保存它时它并没有改变。当我尝试添加一个新的模型实例时,即使我输入了一些东西,它也会清空表单字段,并说 slug 字段是必需的。我在 Django 文档中读到,预填充 slug 字段是自定义的,但它没有在任何地方说明它是必需的。
型号
class WebPage(models.Model):
template = models.CharField(max_length=50, blank=True, null=True, choices=TEMPLATE_CHOICES, default='default')
label = models.CharField(max_length=100)
slug = models.SlugField(unique=True)
meta_title = models.CharField(max_length=100, blank=True, null=True,
help_text='This shows at the top of the browser, usually in the tab.')
meta_description = models.CharField(max_length=180, blank=True, null=True,
help_text='Optimal length is roughly 155 characters')
meta_tags = models.CharField(max_length=500, blank=True, null=True)
billboards = models.ManyToManyField(Billboard, blank=True, null=True)
image_cover = FileBrowseField(max_length=400, blank=True, null=True, help_text='Roughly 1400px by 400px',
verbose_name='Cover image')
header_content = models.TextField(blank=True, null=True)
# template overrides
image_desktop = FileBrowseField(max_length=400, blank=True, null=True,
verbose_name='Desktop logo', help_text='Transparent .png')
image_mobile = FileBrowseField(max_length=400, blank=True, null=True,
verbose_name='Mobile logo', help_text='Transparent .png')
nav_color = models.CharField(max_length=7, blank=True, null=True)
nav_hover_color = models.CharField(max_length=7, blank=True, null=True)
pre_nav_color = models.CharField(max_length=7, blank=True, null=True)
page_header_color = models.CharField(max_length=7, blank=True, null=True)
is_published = models.BooleanField(default=True)
create_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.label
def save(self, *args, **kwargs):
self.slug = slugify(self.slug)
super(WebPage, self).save(*args, **kwargs)
管理员
class WebPageAdmin(admin.ModelAdmin):
model = WebPage
list_display = ('label', 'slug', 'is_published', 'create_date')
list_filter = ('modified_date', 'is_published')
list_editable = ('is_published',)
search_fields = ('label',)
save_as = True
# readonly_fields = ('slug',)
filter_horizontal = ('billboards',)
inlines = [
PageSectionInline,
]
fieldsets = (
(None, {
'classes': ('suit-tab suit-tab-general full-width',),
'fields': ('template', 'label', 'slug', 'billboards', 'is_published')
}),
(None, {
'classes': ('suit-tab suit-tab-overrides full-width',),
'fields': ('image_desktop', 'image_mobile', 'nav_color', 'nav_hover_color', 'pre_nav_color',
'page_header_color')
}),
(None, {
'classes': ('suit-tab suit-tab-seo full-width',),
'fields': ('meta_title', 'meta_description', 'meta_tags', 'slug')
}),
)
suit_form_tabs = (('general', 'General'), ('sections', 'Sections'),
('overrides', 'Template Overrides'), ('seo', 'SEO'))
class Media:
js = [
'//cdn.tinymce.com/4/tinymce.min.js',
'/static/admin_js/tinymce_init.js',
'/static/page/admin/webpage.js'
]
您总是在保存函数中使用现有的 slug 字段,检查值是否有变化。
def save(self, *args, **kwargs):
self.slug = slugify(kwargs.pop('slug', self.slug))
super(WebPage, self).save(*args, **kwargs)
我有一个 Django 应用程序,它的模型有一个 slug 字段。到目前为止,我只是简单地对模型的标签字段进行 slug 处理,并在管理员中将 slug 设置为只读。我的客户现在希望我允许他们自己将 slug 编辑成他们想要的任何内容。我取消了管理员中的只读访问权限,并更改了模型上的保存方法以不使用其 slugified 标签。然而,当我去管理员那里并尝试更改 slug 时,当我保存它时它并没有改变。当我尝试添加一个新的模型实例时,即使我输入了一些东西,它也会清空表单字段,并说 slug 字段是必需的。我在 Django 文档中读到,预填充 slug 字段是自定义的,但它没有在任何地方说明它是必需的。
型号
class WebPage(models.Model):
template = models.CharField(max_length=50, blank=True, null=True, choices=TEMPLATE_CHOICES, default='default')
label = models.CharField(max_length=100)
slug = models.SlugField(unique=True)
meta_title = models.CharField(max_length=100, blank=True, null=True,
help_text='This shows at the top of the browser, usually in the tab.')
meta_description = models.CharField(max_length=180, blank=True, null=True,
help_text='Optimal length is roughly 155 characters')
meta_tags = models.CharField(max_length=500, blank=True, null=True)
billboards = models.ManyToManyField(Billboard, blank=True, null=True)
image_cover = FileBrowseField(max_length=400, blank=True, null=True, help_text='Roughly 1400px by 400px',
verbose_name='Cover image')
header_content = models.TextField(blank=True, null=True)
# template overrides
image_desktop = FileBrowseField(max_length=400, blank=True, null=True,
verbose_name='Desktop logo', help_text='Transparent .png')
image_mobile = FileBrowseField(max_length=400, blank=True, null=True,
verbose_name='Mobile logo', help_text='Transparent .png')
nav_color = models.CharField(max_length=7, blank=True, null=True)
nav_hover_color = models.CharField(max_length=7, blank=True, null=True)
pre_nav_color = models.CharField(max_length=7, blank=True, null=True)
page_header_color = models.CharField(max_length=7, blank=True, null=True)
is_published = models.BooleanField(default=True)
create_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.label
def save(self, *args, **kwargs):
self.slug = slugify(self.slug)
super(WebPage, self).save(*args, **kwargs)
管理员
class WebPageAdmin(admin.ModelAdmin):
model = WebPage
list_display = ('label', 'slug', 'is_published', 'create_date')
list_filter = ('modified_date', 'is_published')
list_editable = ('is_published',)
search_fields = ('label',)
save_as = True
# readonly_fields = ('slug',)
filter_horizontal = ('billboards',)
inlines = [
PageSectionInline,
]
fieldsets = (
(None, {
'classes': ('suit-tab suit-tab-general full-width',),
'fields': ('template', 'label', 'slug', 'billboards', 'is_published')
}),
(None, {
'classes': ('suit-tab suit-tab-overrides full-width',),
'fields': ('image_desktop', 'image_mobile', 'nav_color', 'nav_hover_color', 'pre_nav_color',
'page_header_color')
}),
(None, {
'classes': ('suit-tab suit-tab-seo full-width',),
'fields': ('meta_title', 'meta_description', 'meta_tags', 'slug')
}),
)
suit_form_tabs = (('general', 'General'), ('sections', 'Sections'),
('overrides', 'Template Overrides'), ('seo', 'SEO'))
class Media:
js = [
'//cdn.tinymce.com/4/tinymce.min.js',
'/static/admin_js/tinymce_init.js',
'/static/page/admin/webpage.js'
]
您总是在保存函数中使用现有的 slug 字段,检查值是否有变化。
def save(self, *args, **kwargs):
self.slug = slugify(kwargs.pop('slug', self.slug))
super(WebPage, self).save(*args, **kwargs)