zoukankan      html  css  js  c++  java
  • Django入门4 数据库设计

    创建mxonline虚拟环境

    C:Usersws>mkvirtualenv mxonline

    (mxonline) D:pythondjangostart>pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl

    修改settings.py配置

    创建mxonline数据库

    生成django默认表

    Makemigrations

    migrate

    新建users app

    编写users的model

    # _*_ encoding:utf-8 _*_
    from __future__ import unicode_literals
    
    from django.db import models
    from django.contrib.auth.models import AbstractBaseUser
    
    # Create your models here.
    
    
    class UserProfile(AbstractBaseUser):    # 利用django的默认user表扩展我们的user表
        nick_name = models.CharField(max_length=50, verbose_name=u"昵称", default='')
        birthday = models.DateField(verbose_name=u"生日", null=True, blank=True)
        gender = models.CharField(max_length=5, choices=(("male",""),("female","")), default="femaile")
        address = models.CharField(max_length=100,default=u"")
        mobile = models.CharField(max_length=11, null = True,blank=True)
        image = models.ImageField(upload_to="image/%Y/%m",default=u"image/default.png", max_length=100)
    
        # 要加如下两行,否则会报错 AttributeError: type object 'UserProfile' has no attribute 'USERNAME_FIELD'
        identifier = models.CharField(max_length=40, unique=True)
        USERNAME_FIELD = 'identifier'
    
        class Meta:
            verbose_name = "用户信息"
            verbose_name_plural = verbose_name
    
        def __unicode__(self):
            return self.username
    manage.py@mxonline > makemigrations users
    manage.py@mxonline > migrate users

    新建课程app course app

    manage.py@mxonline > startapp courses

    编写课程models

    course/models.py

    # _*_ coding:utf-8 _*_
    from __future__ import unicode_literals
    from datetime import datetime
    
    from django.db import models
    
    # Create your models here.
    
    
    class Course(models.Model):
        name = models.CharField(max_length=50, verbose_name=u"课程名")
        desc = models.CharField(max_length=300, verbose_name=u"课程描述")
        detail = models.TextField(verbose_name=u"课程详情")
        degree = models.CharField(choices=(("cj","初级"),("zj","中级"),("gj","高级")), max_length=2)
        learn_time = models.IntegerField(default=0, verbose_name=u"学习时长(分钟数)")
        students = models.IntegerField(default=0, verbose_name=u"学习人数")
        fav_nums = models.IntegerField(default=0, verbose_name=u"收藏")
        image = models.ImageField(upload_to="courses/%Y/%m", verbose_name=u"封面图", max_length=100)
        click_nums = models.IntegerField(default=0, verbose_name=u"点击数")
        add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")
    
        class Meta:
            verbose_name = u"课程"
            verbose_name_plural = verbose_name
    
    
    class Lesson(models.Model):
        course = models.ForeignKey(Course, verbose_name=u"课程")
        name = models.CharField(max_length=100, verbose_name=u'章节名')
        add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")
    
        class Meta:
            verbose_name = u"章节"
            verbose_name_plural = verbose_name
    
    
    class Video(models.Model):
        lesson = models.ForeignKey(Lesson, verbose_name=u'章节')
        name = models.CharField(max_length=100, verbose_name=u'视频名称')
        add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")
    
        class Meta:
            verbose_name = u"视频"
            verbose_name_plural = verbose_name
    
    
    class CourseResource(models.Model):
        course = models.ForeignKey(Course, verbose_name=u'课程')
        name = models.CharField(max_length=100, verbose_name=u'名称')
        download = models.FileField(upload_to = "course/resource/%Y/%m", verbose_name=u'资源文件', max_length=100)
        add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")
    
        class Meta:
            verbose_name = u"课程资源"
            verbose_name_plural = verbose_name

    新建组织app

    manage.py@mxonline > startapp organization

    新建操作app

    manage.py@mxonline >  startapp operation

    写好各个models后,进行settings.py配置

    生成表

    manage.py@mxonline > makemigrations

    manage.py@mxonline > migrate

    此时会在每个app下面生成0001_inital.py

    可以看到django生成的表是app+app里面的class全部小写拼接而成

    我们建的app比较多,为了便于管理,我们在项目目录下新建apps包,把users,course,ogranaziton和operation拖入apps中

    把apps加入到编辑器的resource root

    Settings.py中配置加入到搜索目录
    sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
    

     

    这样在命令行下就可以正常运行了

  • 相关阅读:
    阿里P8架构师谈:阿里双11秒杀系统如何设计?
    秒杀系统设计的知识点
    秒杀系统架构优化思路
    秒杀系统解决方案
    Entity Framework Code First (七)空间数据类型 Spatial Data Types
    Entity Framework Code First (六)存储过程
    Entity Framework Code First (五)Fluent API
    Entity Framework Code First (四)Fluent API
    Entity Framework Code First (三)Data Annotations
    Entity Framework Code First (二)Custom Conventions
  • 原文地址:https://www.cnblogs.com/reblue520/p/12048350.html
Copyright © 2011-2022 走看看