0

In my django rest project I have created total 5 apps with corresponding models with their own serializers (Authentication (-> Profile), crc (-> Card, Responsibility, Collaborator), project (-> Project), scenarios (-> Scenario) and tasks (-> Task)). Models also have associations with each other like

class Project(models.Model):
    stakeholders = models.ManyToManyField(User, blank=True)

class Profile(models.Model):
    user = models.OneToOneField(User, blank=True, on_delete=models.CASCADE, )

class Scenario(models.Model):
    stakeholder = models.ForeignKey(User, related_name='scenarios', blank=True,)
    project = models.ForeignKey(Project, related_name='project_scenarios',)
    tasks = models.ManyToManyField('tasks.Task', blank=True)

class Task(models.Model):
    stakeholder = models.ForeignKey(User, related_name='tasks', blank=True, )
    project = models.ForeignKey(Project, related_name='project_tasks' )  

I want to list all the models and their corresponding methods that I have written. I tried

import django.apps
print(django.apps.apps.get_models())

but this lists all the classes it knows about:

[<class 'django.contrib.admin.models.LogEntry'>,
<class 'django.contrib.auth.models.Permission'>,
<class 'django.contrib.auth.models.Group'>,
<class 'django.contrib.auth.models.User'>,
<class 'django.contrib.contenttypes.models.ContentType'>,
<class 'django.contrib.sessions.models.Session'>,
<class 'rest_framework.authtoken.models.Token'>,
<class 'django.contrib.sites.models.Site'>,
<class 'allauth.account.models.EmailAddress'>,
<class 'allauth.account.models.EmailConfirmation'>,
<class 'authentication.models.Profile'>,
<class 'project.models.Project'>,
<class 'tasks.models.Task'>,
<class 'scenarios.models.Scenario'>,
<class 'crc.models.Card'>,
<class 'crc.models.Responsibility'>,
<class 'crc.models.Collaborator'>]

How can I retrieve only those models and their corresponding methods?

UPDATE:

I tried something like:

@api_view(['GET'])
def class_list(request):
    my_apps=['authentication', 'crc', 'moel_views', 'project', 'scenarios', 'tasks']

    my_app_models = {name: apps.all_models[name] for name in my_apps}


    return Response(my_app_models)

but it throws class not JSON serializable error

Thinker
  • 5,326
  • 13
  • 61
  • 137

1 Answers1

1

You are trying to create a JSON response from a structure containing non-serializable entries (namely the <class 'django.contrib.admin.models.LogEntry'>, <class 'django.contrib.auth.models.Permission'>, etc.).

As stated in the linked solution: Django: apps.get_models() yields models from unittests
(Disclaimer: My answer to another question) you are using, the result of the code is to get a dictionary containing a map of application to models:

{'app_name': OrderedDict_of_models}

If you want to get the names of all the models per app as JSON from a Response, you can try the .__name__ attribute of every item in the OrderedDict:

@api_view(['GET'])
def class_list(request):
    my_apps=[
        'authentication', 'crc', 'model_views', 'project', 'scenarios', 'tasks'
    ]
    my_app_models = {name: apps.all_models[name] for name in my_apps}

    response = {}
    for key, values in my_app_models.items():
        response[key] = [
            model_name.__name__ for model_name in my_app_models[key].values()
        ]

    return Response(response)

If you want the attributes of each model, you can try the __dict__ but be careful, because the result won't be JSON Serializable either and you will need to process them.

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
  • Thanks for your answer! Sadly it gave me `'OrderedDict' object has no attribute '__name__'` error – Thinker May 17 '18 at 12:48
  • Thanks a lot! It did work. Is there any api which also returns all the methods of a such a model? – Thinker May 17 '18 at 12:55
  • @Nitish I would suggest this answer (and the `__dict__` I mentioned above): https://stackoverflow.com/questions/1911281/how-do-i-get-list-of-methods-in-a-python-class – John Moutafis May 17 '18 at 12:58