Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import my django app's models from command-line?

Tags:

python

django

I would like to be able to manipulate my Django app's models via the python console. I am able to do this with PyCharm but I do not have access to PyCharm in this scenario. I tried this:

[root@myhost scripts]# source /apps/capman/env/bin/activate
(env) [root@myhost scripts]# python
Python 2.7.14 (default, Jan  9 2018, 20:51:20)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> from vc.models import *

But I get the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named vc.models 

What am I doing wrong?

like image 809
Red Cricket Avatar asked Sep 14 '18 19:09

Red Cricket


People also ask

How import all models in Django?

To do this, create a directory called /«project»/«app_name»/models , inside it put __init__.py (to declare it as a module) and then create your files inside there. You then need to import your file contents into the module in __init__.py . You should read about Python modules to understand this.


2 Answers

You probably need to start a Shell first

python manage.py shell 

Then run your

from vc.models import *
like image 85
dfundako Avatar answered Nov 15 '22 22:11

dfundako


python has a query system called ORM which are python queries based on MYSQL, we can apply these (queriyset) so they are called in django

go to the console and you must go to where your django project is and of course where the manage.py file is located and you will place the following ones:

python manage.py shell

you will notice that the shell will open there, we must import all our models that we want to perform queryset d as follows:

from APPS.models import Class

or

from .models import *
like image 36
jpozzo Avatar answered Nov 15 '22 21:11

jpozzo