我的django项目之前在app中有models.py文件用来保存数据表,现在我在app中新建了一个models_file的文件夹并在这个文件夹中新建了py文件来写入数据表。在执行python3 manage.py makemigrations 和 python3 manage.py migrate后数据库没有更新models_file中的数据库改动,需要怎样做才能使models_file中的数据表生效

时间: 2024-03-16 15:47:39 浏览: 18
默认情况下,Django只会在app的根目录中查找`models.py`文件来生成数据库迁移。如果你在app中创建了一个新的文件夹并在其中创建了一个新的models文件,则需要告诉Django在该文件夹中查找模型。 为此,你需要在你的app的`__init__.py`文件中导入你创建的`models_file`文件夹,以便Django能够找到其中的模型。具体操作步骤如下: 1. 在你的app目录下,打开`__init__.py`文件。 2. 在文件中添加以下代码: ```python # app/__init__.py default_app_config = 'app.apps.AppConfig' ``` 3. 在你的app目录下,创建一个新的`apps.py`文件(如果没有),并添加以下代码: ```python # app/apps.py from django.apps import AppConfig class AppConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'app' # 添加以下代码 def ready(self): import models_file ``` 4. 执行`python3 manage.py makemigrations`和`python3 manage.py migrate`命令,使Django能够检测到并应用你在`models_file`文件夹中创建的模型。 这样,Django就会自动在`models_file`文件夹中查找模型并生成相应的数据库迁移文件了。

相关推荐

Traceback (most recent call last): File "D:\PyCharm Community Edition 2022.3.3\plugins\python-ce\helpers\pydev\pydevconsole.py", line 364, in runcode coro = func() File "<input>", line 1, in <module> File "D:\PyCharm Community Edition 2022.3.3\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import module = self._system_import(name, *args, **kwargs) File "D:\PythonProject\Django_dianshang\utils\loaddata.py", line 2, in <module> from goods.models import * File "D:\PyCharm Community Edition 2022.3.3\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import module = self._system_import(name, *args, **kwargs) File "D:\PythonProject\Django_dianshang\goods\models.py", line 7, in <module> class Category(models.Model): File "D:\anaconda3\envs\djangopro\lib\site-packages\django\db\models\base.py", line 129, in __new__ app_config = apps.get_containing_app_config(module) File "D:\anaconda3\envs\djangopro\lib\site-packages\django\apps\registry.py", line 260, in get_containing_app_config self.check_apps_ready() File "D:\anaconda3\envs\djangopro\lib\site-packages\django\apps\registry.py", line 137, in check_apps_ready settings.INSTALLED_APPS File "D:\anaconda3\envs\djangopro\lib\site-packages\django\conf\__init__.py", line 102, in __getattr__ self._setup(name) File "D:\anaconda3\envs\djangopro\lib\site-packages\django\conf\__init__.py", line 82, in _setup raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Traceback (most recent call last): File "D:\PyCharm Community Edition 2022.3.3\plugins\python-ce\helpers\pydev\pydevconsole.py", line 364, in runcode coro = func() File "<input>", line 1, in <module> File "D:\PyCharm Community Edition 2022.3.3\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import module = self._system_import(name, *args, **kwargs) File "D:\PythonProject\Django_dianshang\dianshang\utils\loaddata.py", line 2, in <module> from dianshang.goods.models import * File "D:\PyCharm Community Edition 2022.3.3\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import module = self._system_import(name, *args, **kwargs) File "D:\PythonProject\Django_dianshang\dianshang\goods\models.py", line 7, in <module> class Category(models.Model): File "D:\anaconda3\envs\djangopro\lib\site-packages\django\db\models\base.py", line 129, in __new__ app_config = apps.get_containing_app_config(module) File "D:\anaconda3\envs\djangopro\lib\site-packages\django\apps\registry.py", line 260, in get_containing_app_config self.check_apps_ready() File "D:\anaconda3\envs\djangopro\lib\site-packages\django\apps\registry.py", line 137, in check_apps_ready settings.INSTALLED_APPS File "D:\anaconda3\envs\djangopro\lib\site-packages\django\conf\__init__.py", line 102, in __getattr__ self._setup(name) File "D:\anaconda3\envs\djangopro\lib\site-packages\django\conf\__init__.py", line 82, in _setup raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

[ERROR][2023-06-08 13:17:01,837][log.py:230]Internal Server Error: /admin/material_scrap/materialscraphead/69/change/ Traceback (most recent call last): File "C:\Users\suzisxue\Anaconda3\envs\django_dev\lib\site-packages\django\core\handlers\exception.py", line 47, in inner response = get_response(request) File "C:\Users\suzisxue\Anaconda3\envs\django_dev\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\suzisxue\Anaconda3\envs\django_dev\lib\site-packages\django\contrib\admin\options.py", line 616, in wrapper return self.admin_site.admin_view(view)(*args, **kwargs) File "C:\Users\suzisxue\Anaconda3\envs\django_dev\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view response = view_func(request, *args, **kwargs) File "C:\Users\suzisxue\Anaconda3\envs\django_dev\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func response = view_func(request, *args, **kwargs) File "C:\Users\suzisxue\Anaconda3\envs\django_dev\lib\site-packages\django\contrib\admin\sites.py", line 232, in inner return view(request, *args, **kwargs) File "C:\Users\suzisxue\Anaconda3\envs\django_dev\lib\site-packages\django\contrib\admin\options.py", line 1660, in change_view return self.changeform_view(request, object_id, form_url, extra_context) File "C:\Users\suzisxue\Anaconda3\envs\django_dev\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper return bound_method(*args, **kwargs) File "C:\Users\suzisxue\Anaconda3\envs\django_dev\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view response = view_func(request, *args, **kwargs) File "C:\Users\suzisxue\Anaconda3\envs\django_dev\lib\site-packages\django\contrib\admin\options.py", line 1540, in changeform_view return self._changeform_view(request, object_id, form_url, extra_context) File "C:\Users\suzisxue\Anaconda3\envs\django_dev\lib\site-packages\django\contrib\admin\options.py", line 1586, in _changeform_view self.save_model(request, new_object, form, not add) File "C:\work\django_app\dj_erp\apps\material_scrap\admin.py", line 138, in save_model reason = request.POST['materialscrapdetail_set-0-reason'] File "C:\Users\suzisxue\Anaconda3\envs\django_dev\lib\site-packages\django\db\models\base.py", line 485, in __init__ _setattr(self, field.name, rel_obj) File "C:\Users\suzisxue\Anaconda3\envs\django_dev\lib\site-packages\django\db\models\fields\related_descriptors.py", line 220, in __set__ self.field.remote_field.model._meta.object_name, ValueError: Cannot assign "<class 'material_scrap.models.MaterialScrapHead'>": "MaterialScrapDetail.serialNum" must be a "MaterialScrapHead" instance.

最新推荐

recommend-type

麦肯锡-年月―中国xx集团战略咨询项目建议书.ppt

麦肯锡-年月―中国xx集团战略咨询项目建议书.ppt
recommend-type

廖倩5.14运营款.xlsx

廖倩5.14运营款.xlsx
recommend-type

setuptools-25.0.2-py2.py3-none-any.whl

文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

全网第一个宝宝生活记录网站root ca

全网第一个宝宝生活记录网站,帮我写一个网站介绍,网址 https://43.136.21.175/ ,二维码分享和登录,统计记录最近时间,功能很全很实用,手机和电脑都可以访问,手机浏览器可以生成快捷方式到桌面和App一样,点右上角可以打开菜单,里面我做了图表统计可以看每天的喂养次数哪些,我们一直在用,很方便的
recommend-type

yes I can.mp3

yes I can.mp3
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。