1.数据库设计
2.插入测试数据
3.配置相关问题
1.数据库设计
数据库有简单的三张表:Article、Category、Tag以下是代码
# -*- coding:utf-8 -*-
from __future__ import unicode_literals from django.db import models class Article(models.Model):
STATUS_CHOICES = (
('d', 'Draft'),
('p', 'Published'),
) title = models.CharField('标题', max_length=64, unique=True)
content = models.TextField('正文')
create_time = models.DateTimeField('创建时间')
last_modified_time = models.DateTimeField('修改时间')
status = models.CharField('文章状态', max_length=1, choices=STATUS_CHOICES)
abstract = models.CharField('摘要', max_length=64, blank=True, null=True, help_text='可选,如若为空将摘取正文的前64个字符')
views = models.PositiveIntegerField('浏览次数', default=0)
likes = models.PositiveIntegerField('点赞数', default=0)
topped = models.BooleanField('置顶', default=False) category = models.ForeignKey('Category', verbose_name='分类', null=True, on_delete=models.SET_NULL)
tags = models.ManyToManyField('Tag', verbose_name='标签集合', blank=True) def __unicode__(self):
return self.title class Meta:
ordering = ['-last_modified_time'] class Category(models.Model):
name = models.CharField('类名', max_length=20)
create_time = models.DateTimeField('创建时间', auto_now_add=True)
last_modified_time = models.DateTimeField('修改时间', auto_now=True) def __unicode__(self):
return self.name class Tag(models.Model):
name = models.CharField('标签名', max_length=20)
create_time = models.DateTimeField('创建时间', auto_now_add=True)
last_modified_time = models.DateTimeField('修改时间', auto_now=True) def __unicode__(self):
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://*.com/questions/6065037/rails-mysql-encoding-issue-question-mysqlerror-illegal-mix-of-collations-l