我有一个Django应用程序,其模型具有slug字段。到目前为止,我一直在简单地模仿模型的标签字段,并且仅在管理员中读取slug。我的客户现在希望我允许他们自己编辑slu to到他们想要的任何东西。我在管理员中取消了readonly访问权限,并更改了我的模型上的save方法,以便不使用其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'
]
答案 0 :(得分:1)
您始终使用保存功能中的现有slug字段,检查值的变化。
def save(self, *args, **kwargs):
self.slug = slugify(kwargs.pop('slug', self.slug))
super(WebPage, self).save(*args, **kwargs)