• 使用django开发博客过程记录1——数据库设计


    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

  • 相关阅读:
    【Todo】Java线程面试题 Top 50 (转载)
    【Todo】秒杀系统 & 乐观锁 & Nginx反向代理
    【Todo】C++和Java里面的浮点数及各种数字表示
    asp.net操作word的表格
    Android消息推送(二)--基于MQTT协议实现的推送功能
    单点更新线段树 RMQ
    英语月结
    AppWidget应用(二)---PendingIntent 之 getActivity
    2 WAN 和1 Evo/3g Routeros PCC 方法负载平衡
    Android 网络权限配置
  • 原文地址:https://www.cnblogs.com/selfimprovement/p/5937360.html
Copyright © 2020-2023  润新知