django 创建model(数据库表)失败

本文详细介绍了在Django项目中创建数据库表时遇到的迁移失败问题,并提供了有效的解决方案,特别是针对INSTALLED_APPS设置中缺少应用名称的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在models.py中创建数据库表
 
 
from django.db import models

# Create your models here.
class Book(models.Model):
    name = models.CharField(max_length=20)
    price = models.IntegerField
    pub_date = models.DateField
 
 
 
在terminal中 :python manage.py makemigrations
 
返回结果:No changes detected
创建失败。
 
原因可能如下:
  1. migration folder You need a migrations package in your app.
  2. INSTALLED_APPS You need your app to be specified in the INSTALLED_APPS .dict
  3. Verbosity start by running makemigrations -v 3 for verbosity. This might shed some light on the problem.
  4. Full path In INSTALLED_APPS it is recommended to specify the full module app config path 'apply.apps.MyAppConfig'
  5. --settings you might want to make sure the correct settings file is set: manage.py makemigrations --settings mysite.settings
  6. specify app name explicitly put the app name in manage.py makemigrations myapp- that narrows down the migrations for the app alone and helps you isolate the problem.
  7. model meta check you have the right app_label in your model meta
  8. Debug django debug django core script. makemigrations command is pretty much straight forward. Here's how to do it in pycharm. change your script definition accordingly (ex: makemigrations --traceback myapp)
 
而我的情况是第二种 :setting.py内的INSTALLED_APPS未添加 app名字
 
 
 
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
     'app1',
]
View Code

 

 

 
添加上后,创建成功
 

 

转载于:https://www.cnblogs.com/huangguoming/p/9990358.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值