[Answered ]-How to refer to model class (name) from html form to views.py with Django?

1👍

from django.conf import settings
from django.apps import apps


def get_all_models():
    model_name_list = []
    installed_apps = settings.INSTALLED_APPS[6:]
    # just exclude system app that is not installed by you
    for app in installed_apps:
        app_config = apps.get_app_config(app)
        for model in app_config.get_models():
            model_dict = dict()
            model_dict['name']: model.__name__
            model_dict['value']: model.__name__.lower()
            model_name_list.append(model_dict)
    return model_name_list

It’s a kind of dynamic way to get all models from different apps in your Django project.

0👍

Thanks to input of @Md.Imam Hossain Roni, I now have an elegant solution to retrieve model of interest from hidden input in HTML form and pass back model information through response.

views.py

from django.apps import apps
from django.conf import settings
from database.utils import xlsx_generator
from django.http import HttpResponse

def get_all_models():
    installed_apps = settings.INSTALLED_APPS[6:]
    model_dict={}
    # just exclude system app that is not installed by you
    for app in installed_apps:
        app_config = apps.get_app_config(app)
        for model in app_config.get_models():
            model_dict[model.__name__] = model
    return model_dict

# Define function to download excel file using template
def download_file(request, filename=''):
    if 'downloadfile' in request.POST:
        db_name = request.POST.get('database')
        model_name_list=get_all_models()
        response = xlsx_generator(model_name_list[db_name], db_name)
        return response

For completeness settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'users',
    'database',
    'project_management',
]

I do wonder though, that if the model base gets very big, if this dictionary has same size of entire sqlite database, and if this will end up being problematic…

Leave a comment