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?
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.
You probably need to start a Shell first
python manage.py shell
Then run your
from vc.models import *
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 *
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With