django3.2连接mysql的视图的时候报错

项目场景:

django3.2连接mysql的视图操作的时候报错:
django.db.utils.OperationalError: (1054, “Unknown column ‘hot.id’ in ‘field list’”)


问题描述:

在app目录里创建了
mysql_view_models.py用于连接mysql数据库里的视图

from .models import *

class Hot(models.Model):
    movieid = models.IntegerField(max_length=50)
    count_rating = models.CharField(max_length=50)
    avg_rating = models.CharField(max_length=50)

    class Meta:
        db_table = 'hot'

结果报错
django.db.utils.OperationalError: (1054, “Unknown column ‘hot.id’ in ‘field list’”)


原因分析:

缺少了id,也就是主键,需要设置主键


解决方案:

在你想要设置的字段里添加参数“primary_key=True”

from .models import *

class Hot(models.Model):
    movieid = models.IntegerField(max_length=50, primary_key=True)
    count_rating = models.CharField(max_length=50)
    avg_rating = models.CharField(max_length=50)

    class Meta:
        db_table = 'hot'

你可能感兴趣的:(Django,django,mysql)