我的项目中包含一个名为 unidecode 的 python 包,它可以很好地解码汉字。但是当我在我的 Django 项目中使用它时,prepopulated_fields 并不能与中文一起使用。
版本信息:django 1.86,Python 3.4
模型.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 #按照标题来显示每个实例,也就是一行数据
管理员.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)