我对 Django 很陌生,我遇到了新模型(以及我制作的新应用程序“博客”)的问题。配置模型和 makemigration 后,表 blog_post 不存在。
这是我所做的所有过程。我正在遵循官方教程:
这是我的博客/models.py:
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=80)
text = models.TextField()
author = models.ForeignKey('auth.User', on_delete= models.CASCADE)
created_date = models.DateTimeField()
pub_date = models.DateTimeField()
def publish():
self.pub_date = timezone.now()
self.save()
我的网站/settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog.apps.BlogConfig',
]
第一个之后
python manage.py makemigrations 博客
Migrations for 'blog':
blog\migrations\0001_initial.py
- Create model Post
python manage.py sqlmigrate 博客 0001
BEGIN;
--
-- Create model Post
--
CREATE TABLE "blog_post" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(80) NOT NULL, "text" text NOT NULL, "created_date" datetime NOT NULL, "pub_date" datetime NOT NULL, "author_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED);
CREATE INDEX "blog_post_author_id_dd7a8485" ON "blog_post" ("author_id");
COMMIT;
python 管理.py 迁移
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
No migrations to apply.
所以,就在这里。新表似乎没有创建。我检查了 SqLite 实用程序,没有这样的表:blog_post
我还检查了 django shell。
我双重(三次)检查该过程:
- 更改您的模型(在 models.py 中)。
- 运行 python manage.py makemigrations 为这些更改创建迁移
- 运行 python manage.py migrate 将这些更改应用到数据库
但我现在被困住了。有人能告诉我我错过了什么吗?谢谢 !
这是我的数据库设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
python 管理.py showmigrations
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
[X] 0009_alter_user_last_name_max_length
blog
[X] 0001_initial
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
sessions
[X] 0001_initial
我使用 SQLite 的数据库浏览器检查表是否存在,但 blog_post 不存在。
github 存储库链接:https://github.com/mothinx/juliengracia