The python package called unidecode which will work well for decoding Chinese characters is included in my project. But when I use it in my Django project, the prepopulated_fields didn't work with Chinese.
Version Information: django 1.86,Python 3.4
Models.py
# coding:utf-8
from django.template.defaultfilters import slugify
from django.db import models
from django.contrib import admin
from unidecode import unidecode
# 类别
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
likes = models.IntegerField(default=0)
slug = models.SlugField(unique=True)
class Meta:
# 定义显示的名字
verbose_name = '类别'
verbose_name_plural = '类别'
def save(self,*args,**kwargs):
self.slug = slugify(unidecode(self.name))
super(Category,self).save(*args,**kwargs)
def __str__(self):
return self.name
class CategoryAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug':(unidecode('name'),)}
# 文章类
class Article(models.Model):
title = models.CharField(u'标题', max_length=256)
content = models.TextField(u'内容')
pub_date = models.DateTimeField(u'发表时间', auto_now_add=True, editable=True)
update_time = models.DateTimeField(u'更新时间',auto_now=True, null=True)
category = models.ForeignKey(Category) #ForeignKey, 创建1对多关系的字段类型.
class Meta:
verbose_name = '文章'
verbose_name_plural = '文章'
def __str__(self):
return self.title #按照标题来显示每个实例,也就是一行数据
admin.py
# coding:utf-8
from django.contrib import admin
from .models import Article, Category ,CategoryAdmin
from django_summernote.admin import SummernoteModelAdmin
class SomeModelAdmin(SummernoteModelAdmin):
pass
# 注册
admin.site.register(Article,SomeModelAdmin)
admin.site.register(Category,CategoryAdmin)
When I input some Chinese characters,I want to see the slug field be auto filled like this:
But the fact is that the slug field is not been auto filled by any characters like this in Django admin:
From the docs:
Since you have no field that corresponds to whatever the output of
unidecode('name')
is, the javascript is probably falling over.Change it to just:
and it should work.