简体   繁体   English

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

[英]How do I use django models.py classes in my own python programs?

So, I want to make some changes in the database using python outsite Django, but I am unable to.因此,我想使用 python 外部 Django 在数据库中进行一些更改,但我做不到。

I'm trying to import the model from models.py but i'm unable to.我正在尝试从 models.py 导入 model 但我无法做到。

from models import NumberInfo
There is this error: 
Traceback (most recent call last):
  File "/home/sagan/p/matematika/matematika/mnumbers/prime_insert.py", line 1, in <module>
    from models import NumberInfo
  File "/home/sagan/p/matematika/matematika/mnumbers/models.py", line 5, in <module>
    class NumberInfo(models.Model):
  File "/home/sagan/.local/lib/python3.9/site-packages/django/db/models/base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/sagan/.local/lib/python3.9/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
    self.check_apps_ready()
  File "/home/sagan/.local/lib/python3.9/site-packages/django/apps/registry.py", line 135, in check_apps_ready
    settings.INSTALLED_APPS
  File "/home/sagan/.local/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/home/sagan/.local/lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/sagan/.local/lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named 'matematika'```

If you want to edit database structure, just edit your models in models.py file and after that run如果要编辑数据库结构,只需在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

Visit Django Documentation on Migrations to find out more!访问Django 迁移文档以了解更多信息!

Make sure you import the django python module and load the settings from the model's project before you try to import the model:在尝试导入 model 之前,请确保导入 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
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM