S
S
sportik1742021-11-21 18:08:45
Python
sportik174, 2021-11-21 18:08:45

How to query django database not from views.py?

Good afternoon!
How to make a database query in Django not from views.py?

For example there is a file which is in test.py file in django application directory
Its content is

from company.models import *

company = Company.objects.all()

print(company)


When I try to run this file with the command , I get an
python3 test.py

error:
Traceback (most recent call last):
  File "company/test.py", line 1, in <module>
    from company.models import *
ModuleNotFoundError: No module named 'company'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-11-21
@sportik174

Django needs to be initialized so that its ORM can be used. But it's better not to do this, most likely there are better ways to solve your problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question