So, I want to make some changes in the database using python outsite Django, but I am unable to.
I'm trying to import the model from models.py but i'm unable to.
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
# 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!
Make sure you import the django python module and load the settings from the model's project before you try to import the model:
# 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
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.