在Django处理应用的数据模块的时候,你可能会遇到如题所示的问题:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS C:\Users\adamhuan> e:
PS E:\> cd "E:\PyCharm_data\Django_Me_G7AI"
PS E:\PyCharm_data\Django_Me_G7AI> dir


    目录: E:\PyCharm_data\Django_Me_G7AI


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2019/6/7     19:10                .idea
d-----         2019/6/7     18:57                Django_Me_G7AI
d-----         2019/6/7     19:02                MicroBlog
d-----         2019/6/7     18:57                templates
d-----         2019/6/7     18:56                venv
-a----         2019/6/7     19:03              0 db.sqlite3
-a----         2019/6/7     18:56            655 manage.py


PS E:\PyCharm_data\Django_Me_G7AI>
PS E:\PyCharm_data\Django_Me_G7AI> python .\manage.py makemigrations
No changes detected
PS E:\PyCharm_data\Django_Me_G7AI>

进一步执行,你就可以看到问题了:

PS E:\PyCharm_data\Django_Me_G7AI> python .\manage.py makemigrations --empty MicroBlog
No installed app with label 'MicroBlog'.
PS E:\PyCharm_data\Django_Me_G7AI>

在Django中,所有的应用,并不是【startapp xxx】创建了就算包含在Django的项目中了的。

Django中的项目,需要在【settings.py】中注册:

文件:settings.py

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'MicroBlog',
]

然后再做【makemigrations】

先看看当前目录的状态

PS E:\PyCharm_data\Django_Me_G7AI> pwd

Path
----
E:\PyCharm_data\Django_Me_G7AI


PS E:\PyCharm_data\Django_Me_G7AI>
PS E:\PyCharm_data\Django_Me_G7AI> dir


    目录: E:\PyCharm_data\Django_Me_G7AI


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2019/6/7     19:17                .idea
d-----         2019/6/7     19:16                Django_Me_G7AI
d-----         2019/6/7     19:02                MicroBlog
d-----         2019/6/7     18:57                templates
d-----         2019/6/7     18:56                venv
-a----         2019/6/7     19:03              0 db.sqlite3
-a----         2019/6/7     18:56            655 manage.py


PS E:\PyCharm_data\Django_Me_G7AI>
PS E:\PyCharm_data\Django_Me_G7AI> dir .\MicroBlog\


    目录: E:\PyCharm_data\Django_Me_G7AI\MicroBlog


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2019/6/7     18:57                migrations
-a----         2019/6/7     18:57             66 admin.py
-a----         2019/6/7     18:57             98 apps.py
-a----         2019/6/7     19:02            517 models.py
-a----         2019/6/7     18:57             63 tests.py
-a----         2019/6/7     18:57             66 views.py
-a----         2019/6/7     18:57              0 __init__.py


PS E:\PyCharm_data\Django_Me_G7AI>
PS E:\PyCharm_data\Django_Me_G7AI> dir .\MicroBlog\migrations\


    目录: E:\PyCharm_data\Django_Me_G7AI\MicroBlog\migrations


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         2019/6/7     18:57              0 __init__.py


PS E:\PyCharm_data\Django_Me_G7AI>

执行【makemigrations】

PS E:\PyCharm_data\Django_Me_G7AI> python .\manage.py makemigrations
System check identified some issues:

WARNINGS:
MicroBlog.MicroBlogPost.createtime: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
Migrations for 'MicroBlog':
  MicroBlog\migrations\0001_initial.py
    - Create model MicroBlogPost
PS E:\PyCharm_data\Django_Me_G7AI>

在看看目录的变化:

PS E:\PyCharm_data\Django_Me_G7AI> dir


    目录: E:\PyCharm_data\Django_Me_G7AI


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2019/6/7     19:20                .idea
d-----         2019/6/7     19:16                Django_Me_G7AI
d-----         2019/6/7     19:20                MicroBlog
d-----         2019/6/7     18:57                templates
d-----         2019/6/7     18:56                venv
-a----         2019/6/7     19:03              0 db.sqlite3
-a----         2019/6/7     18:56            655 manage.py


PS E:\PyCharm_data\Django_Me_G7AI>
PS E:\PyCharm_data\Django_Me_G7AI> dir .\MicroBlog\


    目录: E:\PyCharm_data\Django_Me_G7AI\MicroBlog


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2019/6/7     19:20                migrations
d-----         2019/6/7     19:20                __pycache__
-a----         2019/6/7     18:57             66 admin.py
-a----         2019/6/7     18:57             98 apps.py
-a----         2019/6/7     19:20            515 models.py
-a----         2019/6/7     18:57             63 tests.py
-a----         2019/6/7     18:57             66 views.py
-a----         2019/6/7     18:57              0 __init__.py


PS E:\PyCharm_data\Django_Me_G7AI>
PS E:\PyCharm_data\Django_Me_G7AI> dir .\MicroBlog\migrations\


    目录: E:\PyCharm_data\Django_Me_G7AI\MicroBlog\migrations


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2019/6/7     19:20                __pycache__
-a----         2019/6/7     19:20           1110 0001_initial.py
-a----         2019/6/7     18:57              0 __init__.py


PS E:\PyCharm_data\Django_Me_G7AI>

可以看到,应用的【migrations】目录,增加了一个【0001_initial.py】的文件。

查看一下:

PS E:\PyCharm_data\Django_Me_G7AI> python .\manage.py sqlmigrate MicroBlog 0001
System check identified some issues:

WARNINGS:
MicroBlog.MicroBlogPost.createtime: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
BEGIN;
--
-- Create model MicroBlogPost
--
CREATE TABLE "MicroBlog_microblogpost" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "post_id" integer NOT NULL, "body" text NOT NULL, "createtime" datetime NOT NULL, "author_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED);
CREATE INDEX "MicroBlog_microblogpost_author_id_a887e69e" ON "MicroBlog_microblogpost" ("author_id");
COMMIT;
PS E:\PyCharm_data\Django_Me_G7AI>

然后,真正创建数据库:【migrate】

PS E:\PyCharm_data\Django_Me_G7AI> python .\manage.py migrate
System check identified some issues:

WARNINGS:
MicroBlog.MicroBlogPost.createtime: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
Operations to perform:
  Apply all migrations: MicroBlog, admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying MicroBlog.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK
PS E:\PyCharm_data\Django_Me_G7AI>

可以看到,这样就没问题了。

————————————

总结一下,本文文首的问题产生是因为:没有在Django的settings.py中注册INSTALLED_APP导致的。

因此,在Django中配置数据模型需要注意这几点:
1. 数据库连接配置正确
2. 目标应用需要在Django的settings.py的INSTALLED_APP中注册
3. models.py语法正确,没有问题

4. Django的目标应用目录下没有【migrations】目录,因此也可能出现【No changes detected】的错误;这时候,应该执行【python manage.py makemigrations –empty xxxAPP_Namexxx】,就可以自动的在目标应用目录下生成【migrations】目录了

————————————
Done。

说点什么

avatar

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

  Subscribe  
提醒