magopian / django-inspect-model

Model inspection for Django

Home Page:http://django-inspect-model.rtfd.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Expose field types

thedrow opened this issue · comments

Getting the field names is nice but I'd like to get the field types as well.

I agree. This would be immensely useful. I am specifically looking for a method to determine whether a field is a FileField and what storage it uses.

I have found a way get this info without this app. I'd say this app is obsolete. Here is an example on a command traversing the information:

from django.core.management.base import BaseCommand
from django.apps import apps
from django.db import models


class Command(BaseCommand):
    help = 'List all FieldFields in models'

    def handle(self, *args, **options):

        for model in apps.get_models():

            for field_name in model._meta.get_all_field_names():
                field = model._meta.get_field(field_name)

                if isinstance(field, models.FileField):
                    print(model.__name__ + ':: ' + str(field_name) + ':\t' + type(field).__name__)

It's not obsolete if you want to keep compatibility between Django versions