Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在自己的python程序中使用django models.py类?_Python_Django_Database_Code Snippets - Fatal编程技术网

如何在自己的python程序中使用django models.py类?

如何在自己的python程序中使用django models.py类?,python,django,database,code-snippets,Python,Django,Database,Code Snippets,因此,我想使用python outsite Django对数据库进行一些更改,但我无法做到 from models import NumberInfo 我正在尝试从models.py导入模型,但无法 from models import NumberInfo 出现以下错误: 回溯(最近一次呼叫最后一次): 文件“/home/sagan/p/matematika/matematika/mnumbers/prime_insert.py”,第1行,在 从型号导入编号信息 文件“/home/saga

因此,我想使用python outsite Django对数据库进行一些更改,但我无法做到

from models import NumberInfo
我正在尝试从models.py导入模型,但无法

from models import NumberInfo
出现以下错误:
回溯(最近一次呼叫最后一次):
文件“/home/sagan/p/matematika/matematika/mnumbers/prime_insert.py”,第1行,在
从型号导入编号信息
文件“/home/sagan/p/matematika/matematika/mnumbers/models.py”,第5行,在
类别编号信息(models.Model):
文件“/home/sagan/.local/lib/python3.9/site packages/django/db/models/base.py”,第108行,在新的__
app\u config=apps.get\u包含app\u config(模块)
文件“/home/sagan/.local/lib/python3.9/site packages/django/apps/registry.py”,第253行,在包含应用程序配置的get_中
self.check_apps_ready()
文件“/home/sagan/.local/lib/python3.9/site packages/django/apps/registry.py”,第135行,在check\u apps\u ready中
settings.INSTALLED\u应用程序
文件“/home/sagan/.local/lib/python3.9/site packages/django/conf/_init__.py”,第82行,在_getattr中__
自我设置(名称)
文件“/home/sagan/.local/lib/python3.9/site packages/django/conf/_init__.py”,第69行,在安装程序中
自包装=设置(设置模块)
文件“/home/sagan/.local/lib/python3.9/site packages/django/conf/_init__.py”,第170行,在_init中__
mod=importlib.import\u模块(自我设置\u模块)
导入模块中的文件“/usr/lib/python3.9/importlib/_init__uu.py”,第127行
return _bootstrap._gcd_import(名称[级别:],包,级别)
ModuleNotFoundError:没有名为“matematika”的模块```

在尝试导入模型之前,请确保导入django python模块并从模型的项目加载设置:

# add the django project to python's path so python can find it
# the variable path_of_python_project is the full path of the django project folder
sys.path.append(path_of_django_project)

# load django and the settings from your project
# I'm pretending your project is called django_projectname, you'll want to update that
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_projectname.settings')

# setup django
django.setup()

# import your model and use it, just like you would in django
from django_projectname.models import NumberInfo
...

如果要编辑数据库结构,只需在
models.py
文件中编辑模型,然后运行

# creating new migrations based on the changes you have made to your models
$ python manage.py makemigrations

# applying and unapplying migrations
$ python manage.py migrate

访问了解更多信息

请提供您的项目结构,并说明错误是如何产生的。您好,Django提供了一种创建管理命令的方法,可以帮助您从外部修改数据库。已经提供了makemigrations、findstatic、shell等命令。请通过链接为您自己创建相同的命令。您不能这样导入,您可以从您的projectname.models import NumberInfo或从您的appname.models import NumberInfo执行>