Is it possible to get django info without running it

I have a django model, the whole code is completed. but I want to access my model info. a code like this to get field names.

for f in myModel._meta.fields:
    print(f.get_attname())

is it possible to do it from an external python script without running django server?

other possible automated ways of doing this and saving results to a file are also appreciated.

thanks to @klaus D and management command documentation I made this structure

users/
    __init__.py
    models.py
    management/
        __init__.py
        commands/
            __init__.py
            _private.py
            modelInfo.py

and in modelInfo.py I did

from django.core.management.base import BaseCommand, CommandError
from users import views2
def savelisttxtfile(the_list, path_, type_='w', encoding="utf-8"):
    with open(path_, type_, encoding=encoding) as file_handler:
        for item in the_list:
            file_handler.write("{}\n".format(item))
class Command(BaseCommand):
    def handle(self, *args, **options):
        dic=[]
        for f in views2.ChertModel._meta.fields:
            print(f.get_attname())
            dic.append(f.get_attname())
        savelisttxtfile(dic,"F:\projects\sd.txt")

and to run it I went to manage.py location and executed python manage.py modelInfo to launch it.

Regarding your "try1" it seems to be a little bit trickier to start a python shell like python manage.py shell than what you propose there.

Fortunately you can do this:

python manage.py shell < your_script.py

and your script will be executed as if typed directly into the "django shell". Keep in mind that you still need to import your models relative to your project, i.e. from myapp.models import mymodel.

Back to Top