Django Prepopulated字段与两个/三个字母的单词 [英] Django Prepopulated Fields with two/three letter words
问题描述
在我的admin.py文件中,我试图使用Prepopulated_fields来自动删除标题。但是似乎有短的两个字母的问题。当我输入Is,the或to时,将其从lug中删除。另外我试过之前,第二个我打了E按钮,它也把它删除了。这是预期的预填字段还是我做错了?
In my admin.py file I am trying to use Prepopulated_fields to auto slug a title. It seems to have problems with short two letter words however. When I type in "Is", "the" or "to", it blanks it out of the slug. Also I tried "before" and the second I hit the E button, it blanked that out too. Is this expected with prepopulated fields or am I doing something wrong?
Models.py
Models.py
title = models.CharField(max_length=255)
entry = models.TextField()
date_edited = models.DateTimeField(auto_now=True, verbose_name = "Last edited")
date_posted = models.DateTimeField(verbose_name="Post Date")
slug = models.SlugField(max_length=255, unique=True)
Admin.py
prepopulated_fields = {"slug": ("title",)}
推荐答案
像@Alasdair在他的回答中说,是使用prepoulated_fields时被忽略的单词。而不是覆盖处理这些单词的js文件,而是从save_model中的标题中转移了slug字段。
Like @Alasdair said in his answer, there are words that get ignored when you use prepoulated_fields. Instead of overriding the js file that handles those words I turned the slug field from the title in the save_model.
def save_model(self, request, obj, form, change):
if not change:
obj.slug = slugify(('%s') % obj.title)
obj.save()
这篇关于Django Prepopulated字段与两个/三个字母的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!