数据表创建不成功

来源:9-10 登录验证客户用户表视频表创建外链视频功能开发-上(3)

慕仔2389771

2020-04-06

我在学习 《9-10 登录验证客户用户表视频表创建外链视频功能开发-上(3)》这个章节,跟着老师编写的完了models文件,现在需要执行 makemigrations 和migrate 出现了问题

这是我的 /project/app/models/auth.py

# coding: utf-8
from  django.db import  models
import hashlib

def hash_password(username,password):
    if isinstance(password,str):
        password = password.encode('utf-8')
    sha256 = hashlib.sha256(username.encode('utf-8'))
    sha256.update(password)
    return sha256.hexdigest()


class ClientUser(models.Model):
    username = models.CharField(max_length=50,null=False,unique=True)
    password = models.CharField(max_length=255,null=False)
    avatar = models.CharField(max_length=500,default='')
    gender = models.CharField(max_length=10,default='')
    birthday = models.DateTimeField(null=True, blank=True, default=None)
    status = models.BooleanField(default=True,db_index=True)
    create_time = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return '%s,%s,%s'%(self.username,self.password,self.status)

    @classmethod
    def add(cls, username,password,avatar='',gender='',birthday=None):
        __password = hash_password(username,password)
        return cls.objects.create(
            username=username,
            password=__password,
            avatar=avatar,
            gender=gender,
            birthday=birthday
        )
    @classmethod
    def get_user(cls,username, password):
        try:
            __password = hash_password(username, password)
            user = cls.objects.get(
                username=username,
                password=__password
            )
            return user
        except:
            return None

    def update_password(self,username, old_password, new_password):
        __password = hash_password(username,old_password)
        if __password != self.password:
            return False
        __new_password = hash_password(username,new_password)
        self.password = __new_password
        self.save()
        return True
    def update_status(self):
        self.status = not self.status
        self.save()
        return True

/project/app/models/video.py

# coding: utf-8
from  enum import Enum
from django.db import models
class VideoType(Enum):
    movie = 'movie'
    cartoon = 'cartoon'
    episode = 'episode'
    variety = 'variety'
    other = 'other'
VideoType.movie.label = '电影'
VideoType.cartoon.label = '动漫'
VideoType.episode.label = '剧集'
VideoType.variety.label = '综艺'
VideoType.other.label = '其他'

class FromType(Enum):
    youku = 'youku'
    custom = 'custom'

FromType.custom.label = '自制'
FromType.youku.label = '优酷'

class Country(Enum):
    china = 'china'
    japan = 'japan'
    korea = 'korea'
    america = 'america'
    other = 'other'


Country.china.label = '中国
Country.japan.label = '日本'
Country.korea.label = '韩国'
Country.america.label = '美国'
Country.other.label = '其他'




class Video(models.Model):
    name = models.CharField(max_length=100,null=False)
    image = models.CharField(max_length=500,default='')
    video_type = models.CharField(max_length=50,default=VideoType.other.value)
    from_to = models.CharField(max_length=20,null=False,default=FromType.custom.value)
    nationality = models.CharField(max_length=20,default=Country.other.value)
    info = models.TextField()
    status = models.BooleanField(default=True,db_index=True)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)
    class Meta:
        unique_together = ('name','video_type','from_to','Country')
    def __str__(self):
        return '%s'%self.name

class VideoStar(models.Model):
    video = models.ForeignKey(
        Video,
        on_delete=models.SET_NULL,
        related_name='video_star',
        blank=True,null=True)
    name = models.CharField(max_length=100,null=False)
    identity = models.CharField(max_length=50, default='')

    class Meta:
        unique_together = ('video','name','identity')

    def __str__(self):
        return self.name

class VideoSub(models.Model):
    video = models.ForeignKey(Video,on_delete=models.SET_NULL,
                              related_name='video_sub',
                              blank=True,null=True)
    url = models.CharField(max_length=500,null=False)
    number = models.IntegerField(default=1)

    class Meta:
        unique_together = ('video','number',)
    def __str__(self):
        return 'video:%s,Number:%s' %(self.video,self.number)


这是报错信息

PS C:\Users\meleuo\Github\project_one\video> python.exe .\manage.py makemigrations
No changes detected
PS C:\Users\meleuo\Github\project_one\video> python.exe .\manage.py  migrate
System check identified some issues:

WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
        HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, b
y escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/2.
1/ref/databases/#mysql-sql-mode
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.
PS C:\Users\meleuo\Github\project_one\video>

我尝试的方法
1:我尝试重建了数据库 × 还是一样报错
2:我尝试在 setting.py 文件中databases 部分添加如下配置 × 没有报错了,但是数据表并没有创建出来

          'OPTIONS': {
              "init_command": "SET sql_mode='STRICT_TRANS_TABLES'",                                                              
          }
请老师帮我看一下
写回答

2回答

deweizhang

2020-04-06

你这里没有报错而是说没有数据库需要更新 我建议你在models文件和文件夹的名字仔细核对一下~

0
2
慕仔2389771
非常感谢!
2020-04-06
共2条回复

慕仔2389771

提问者

2020-04-06

问题解决,/project/app/model 这个文件夹我起的名字是/project/app/models 可能是和models.py冲突了

0
0

Django入门到进阶-适合Python小白的系统课程

入门Django的同时,让你形成更贴近实际工作的Python Web开发知识体系

1114 学习 · 464 问题

查看课程