1.数据库设计
2.插入测试数据
3.配置相关问题
1.数据库设计
数据库有简单的三张表:Article、Category、Tag以下是代码
1 # -*- coding:utf-8 -*- 2 from __future__ import unicode_literals 3 4 from django.db import models 5 6 7 class Article(models.Model): 8 STATUS_CHOICES = ( 9 ('d', 'Draft'), 10 ('p', 'Published'), 11 ) 12 13 title = models.CharField('标题', max_length=64, unique=True) 14 content = models.TextField('正文') 15 create_time = models.DateTimeField('创建时间') 16 last_modified_time = models.DateTimeField('修改时间') 17 status = models.CharField('文章状态', max_length=1, choices=STATUS_CHOICES) 18 abstract = models.CharField('摘要', max_length=64, blank=True, null=True, help_text='可选,如若为空将摘取正文的前64个字符') 19 views = models.PositiveIntegerField('浏览次数', default=0) 20 likes = models.PositiveIntegerField('点赞数', default=0) 21 topped = models.BooleanField('置顶', default=False) 22 23 category = models.ForeignKey('Category', verbose_name='分类', null=True, on_delete=models.SET_NULL) 24 tags = models.ManyToManyField('Tag', verbose_name='标签集合', blank=True) 25 26 def __unicode__(self): 27 return self.title 28 29 class Meta: 30 ordering = ['-last_modified_time'] 31 32 class Category(models.Model): 33 name = models.CharField('类名', max_length=20) 34 create_time = models.DateTimeField('创建时间', auto_now_add=True) 35 last_modified_time = models.DateTimeField('修改时间', auto_now=True) 36 37 def __unicode__(self): 38 return self.name 39 40 class Tag(models.Model): 41 name = models.CharField('标签名', max_length=20) 42 create_time = models.DateTimeField('创建时间', auto_now_add=True) 43 last_modified_time = models.DateTimeField('修改时间', auto_now=True) 44 45 def __unicode__(self): 46 return self.name
2.插入测试数据
此时登录admin后台没有数据,于是想利用编写一个populate.py负责插入测试数据以下是代码:
# -*- coding:utf-8 -*- import datetime import os os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pureblog.settings') import django from django.core.exceptions import ObjectDoesNotExist django.setup() from apps.models import Article, Category, Tag def populate(): tags = [] cat_Python = add_cat('Python') tag_django = add_tag('django') tags.append(tag_django) add_article('标题2', 'p', cat_Python, tags) def add_article(title, status, cat, tags): a = Article.objects.get_or_create(title=title, create_time=datetime.datetime.utcnow(), last_modified_time=datetime.datetime.utcnow(), status=status)[0] a.content = '这是测试数据' a.abstract = a.content[:64] a.category = cat for tag in tags: a.tags.add(tag) a.save() return a def add_cat(name): try: obj = Category.objects.get(name=name) except ObjectDoesNotExist: obj = Category(name=name, create_time=datetime.datetime.utcnow()) obj.save() return obj def add_cat(name): try: obj = Category.objects.get(name=name) except ObjectDoesNotExist: obj = Category(name=name, create_time=datetime.datetime.utcnow()) obj.save() return obj def add_tag(name): try: obj = Tag.objects.get(name=name) except ObjectDoesNotExist: obj = Tag(name=name, create_time=datetime.datetime.utcnow()) obj.save() return obj if __name__ == '__main__': populate()
3.配置相关问题
1)因为我的数据库使用的是mysql数据库,插入数据时可能会遇到字符编码同问题,以下链接是解决这个问题的链接:
http://stackoverflow.com/questions/6065037/rails-mysql-encoding-issue-question-mysqlerror-illegal-mix-of-collations-l