0

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'```
3
  • would you provide your project structure and also mention how did the error produced. Commented Jan 31, 2021 at 20:55
  • Hi, Django provide a way to create admin command that will help you to modify database from outside. There are already provided commands like makemigrations, findstatic, shell, etc. please go through link docs.djangoproject.com/en/3.1/howto/custom-management-commands to create same for yourself Commented Feb 1, 2021 at 0:44
  • You can't import like that, you can do > from yourprojectname.models import NumberInfo or from yourappname.models import NumberInfo Commented Feb 1, 2021 at 8:09

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.