Django 4.2.1 with django-cookiecutter 2023.05.09: makemigrations doesn't create migration file for new model

huangapple go评论74阅读模式
英文:

Django 4.2.1 with django-cookiecutter 2023.05.09: makemigrations doesn't create migration file for new model

问题

以下是您要翻译的内容:

"I'm using Django 4.2.1 with the latest version of django-cookiecutter (2023.05.09) on MacOS Ventura 13.3.1, and I'm having trouble getting the makemigrations command to create a migration file for a new model I've added to my Django project.

Here's what I've done thus far:

I added the new model to my models.py file in my app directory:

  1. from django.db import models
  2. class TestModel(models.Model):
  3. name = models.CharField(max_length=50)

I created an empty migration file for the app with the makemigrations command:

  1. python manage.py makemigrations myapp --empty

I ran the makemigrations command again to create a migration for the new model, but it didn't create a new migration file:

  1. python manage.py makemigrations

I've double-checked that the TestModel app is included in my INSTALLED_APPS list in my settings.py file, and I've checked that there are no syntax errors in my models.py file.

I've also tried running the check command to see if there are any issues with my code, but it doesn't show any problems:

  1. python manage.py check

I've also tried running the makemigrations command with the --verbosity 3 option to get more detailed output, but it doesn't show any errors or warnings related to the TestModel app.

I've also checked that the migration file has the correct name and is located in the correct directory. The migration file is named 0001_initial.py and is located in the myapp/migrations/ directory.

This is what my directory tree looks like:

[Directory tree]

What could be causing makemigrations to not create a migration file for my new model? Are there any additional steps I can take to diagnose the problem? Any help would be appreciated!"

英文:

I'm using Django 4.2.1 with the latest version of django-cookiecutter (2023.05.09) on MacOS Ventura 13.3.1, and I'm having trouble getting the makemigrations command to create a migration file for a new model I've added to my Django project.

Here's what I've done thus far:

I added the new model to my models.py file in my app directory:

  1. from django.db import models
  2. class TestModel(models.Model):
  3. name = models.CharField(max_length=50)

I created an empty migration file for the app with the makemigrations command:

  1. python manage.py makemigrations myapp --empty

I ran the makemigrations command again to create a migration for the new model, but it didn't create a new migration file:

  1. python manage.py makemigrations

I've double-checked that the TestModel app is included in my INSTALLED_APPS list in my settings.py file, and I've checked that there are no syntax errors in my models.py file.

I've also tried running the check command to see if there are any issues with my code, but it doesn't show any problems:

  1. python manage.py check

I've also tried running the makemigrations command with the --verbosity 3 option to get more detailed output, but it doesn't show any errors or warnings related to the TestModel app.

I've also checked that the migration file has the correct name and is located in the correct directory. The migration file is named 0001_initial.py and is located in the myapp/migrations/ directory.

This is what my directory tree looks like:

  1. └─ base
  2. ├─ .dockerignore
  3. ├─ .editorconfig
  4. ├─ .envs
  5. ├─ .local
  6. ├─ .django
  7. └─ .postgres
  8. └─ .production
  9. ├─ .django
  10. └─ .postgres
  11. ├─ .pre-commit-config.yaml
  12. ├─ .readthedocs.yml
  13. ├─ CONTRIBUTORS.txt
  14. ├─ LICENSE
  15. ├─ MailHog
  16. ├─ README.md
  17. ├─ SECURITY.md
  18. ├─ base
  19. ├─ __init__.py
  20. ├─ conftest.py
  21. ├─ contrib
  22. ├─ __init__.py
  23. └─ sites
  24. ├─ __init__.py
  25. └─ migrations
  26. ├─ 0001_initial.py
  27. ├─ 0002_alter_domain_unique.py
  28. ├─ 0003_set_site_domain_and_name.py
  29. ├─ 0004_alter_options_ordering_domain.py
  30. ├─ __init__.py
  31. ├─ media
  32. ├─ static
  33. ├─ css
  34. ├─ fonts
  35. ├─ images
  36. ├─ js
  37. ├─ sass
  38. └─ webpack_bundles
  39. ├─ templates
  40. ├─ 403.html
  41. ├─ 404.html
  42. ├─ 500.html
  43. ├─ account
  44. ├─ account_inactive.html
  45. ├─ base.html
  46. ├─ email.html
  47. ├─ email_confirm.html
  48. ├─ login.html
  49. ├─ logout.html
  50. ├─ password_change.html
  51. ├─ password_reset.html
  52. ├─ password_reset_done.html
  53. ├─ password_reset_from_key.html
  54. ├─ password_reset_from_key_done.html
  55. ├─ password_set.html
  56. ├─ signup.html
  57. ├─ signup_closed.html
  58. ├─ verification_sent.html
  59. └─ verified_email_required.html
  60. ├─ base.html
  61. ├─ pages
  62. ├─ about.html
  63. └─ home.html
  64. └─ users
  65. ├─ user_detail.html
  66. └─ user_form.html
  67. ├─ users
  68. ├─ __init__.py
  69. ├─ adapters.py
  70. ├─ admin.py
  71. ├─ api
  72. ├─ apps.py
  73. ├─ context_processors.py
  74. ├─ forms.py
  75. ├─ managers.py
  76. ├─ migrations
  77. ├─ 0001_initial.py
  78. ├─ __init__.py
  79. ├─ models.py
  80. ├─ tasks.py
  81. ├─ urls.py
  82. └─ views.py
  83. ├─ utils
  84. ├─ __init__.py
  85. └─ storages.py
  86. └─ website
  87. ├─ __init__.py
  88. ├─ __pycache__
  89. ├─ admin.py
  90. ├─ apps.py
  91. ├─ migrations
  92. ├─ 0001_initial.py
  93. ├─ __init__.py
  94. ├─ models
  95. ├─ __init__.py
  96. ├─ models.py
  97. ├─ tests.py
  98. ├─ urls.py
  99. └─ views.py
  100. ├─ config
  101. ├─ __init__.py
  102. ├─ api_router.py
  103. ├─ asgi.py
  104. ├─ celery_app.py
  105. ├─ settings
  106. ├─ __init__.py
  107. ├─ base.py
  108. ├─ local.py
  109. ├─ production.py
  110. └─ test.py
  111. ├─ urls.py
  112. ├─ websocket.py
  113. └─ wsgi.py
  114. ├─ docs
  115. ├─ dump.rdb
  116. ├─ local.yml
  117. ├─ locale
  118. └─ README.rst
  119. ├─ manage.py
  120. ├─ merge_production_dotenvs_in_dotenv.py
  121. ├─ package-lock.json
  122. ├─ package.json
  123. ├─ production.yml
  124. ├─ pyproject.toml
  125. ├─ requirements
  126. ├─ setup.cfg
  127. ├─ staticfiles
  128. ├─ tests
  129. ├─ webpack
  130. └─ webpack-stats.json

What could be causing makemigrations to not create a migration file for my new model? Are there any additional steps I can take to diagnose the problem? Any help would be appreciated!

答案1

得分: 0

我没有尝试过,但有一个模型文件夹和模型文件...也许这导致找不到model.py:

  1. ├─ models
  2. ├─ __init__.py
  3. ├─ models.py

根据你的问题,不清楚你在哪个应用/模型下遇到了问题。

在那个特定应用的初始迁移文件中有什么内容?

英文:

I did not try it but there is a model folder and model file ... maybe that causes model.py to not be found:

  1. ├─ models
  2. ├─ __init__.py
  3. ├─ models.py

from you question it is not clear which app/models you are having a problem with.

What is in the inital migration file of that specific app?

答案2

得分: 0

要创建一个新的Django应用程序,我建议使用python manage.py startapp <app_name>命令,而不是django_admin startapp <app_name>python manage.py命令是一个方便的包装器,提供额外的功能并与Django项目更好地集成。

startapp命令用于在项目中创建一个新的Django应用程序。通过运行python manage.py startapp <app_name>,Django将为您的应用程序生成必要的文件和目录。这包括应用程序的模型、视图、模板和其他组件。

英文:

To create a new Django app, I recommend using the python manage.py startapp &lt;app_name&gt; command instead of django_admin startapp &lt;app_name&gt;. The python manage.py command is a convenience wrapper that provides additional functionality and integrates better with Django projects.

The startapp command is used to create a new Django app within your project. By running python manage.py startapp &lt;app_name&gt;, Django will generate the necessary files and directories for your app. This includes the app's models, views, templates, and other components.

huangapple
  • 本文由 发表于 2023年5月13日 11:20:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定