-1

If I call this django method, in a test, it yields a lot of models which are not installed.
These models are from other apps test code.

For example, when iI use apps.get_models() I get MROBase1 from the django package polymorphic test code.

=> I want get all models which have a table in the database. In above question I got a model which exists just for testing, which is not on the database.

NB: I use Django 1.10

Dhia
  • 10,119
  • 11
  • 58
  • 69
guettli
  • 25,042
  • 81
  • 346
  • 663
  • After the clarification provided at @Anupam 's answer, I have edited my answer accordingly. Have a look and please try toe dit your answer, as to be more clear about your intentions. – John Moutafis Dec 14 '17 at 14:21

3 Answers3

5

You need to isolate the models from your application(s):

  1. Create manually, a list of all your application names as strings: my_apps=['my_app_1', 'my_app_2', ...]

  2. (First Option), use get_app_config and get_models methods:

    from django.apps import apps
    
    my_app_models = {
        name: list(apps.get_app_config(name).get_models()) for name in my_apps
    }
    

    You will end up with a dictionary of 'app_name': list_of_models

  3. (Second Option), use all_models[<app_name>] attribute:

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

    You will end up with a dictionary of 'app_name': OrderedDict_of_models

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
  • I don't have the initial list of my_app. Can I use settings.INSTALLED_APPS for this? – guettli Dec 15 '17 at 08:33
  • @guettli Yes you could use INSTALLED_APPS but you may get third party models on your list if they are installed in there. – John Moutafis Dec 15 '17 at 08:37
  • I want get all models which have a table in the database. In above question I got a model which exists just for testing, which is not on the database. – guettli Dec 15 '17 at 11:55
  • @guettli Is it on your installed apps? – John Moutafis Dec 15 '17 at 11:57
  • please wait a second I check the question again ......... I read "These models are from other apps test code.". With other words the app polymorphic is in INSTALLED_APPS, but the models from the test code are not in the normal DB. That's what the question is a about: Why do models from test cases get returned if I call apps.get_models(). – guettli Dec 15 '17 at 13:55
  • @guettli Because at the moment of the test, a test_db get's created and migrations applied to it. In this test_db the test code models are applied: https://docs.djangoproject.com/en/2.0/topics/testing/overview/#the-test-database. Finally, you can get your app names, by writing down your app folder names (they should be the same as your app names if you didn't do a massive manual change...). I can add this to the answer if this is what you are asking. – John Moutafis Dec 15 '17 at 14:11
  • @JohnMoutafis I tried this approach but it gave me ` is not JSON serializable` error – Thinker May 17 '18 at 12:09
  • @Nitish without a complete view of your code I cannot help you. If you have a persistent problem, consider creating a new question with details. – John Moutafis May 17 '18 at 12:12
  • @JohnMoutafis kindly refer to this: https://stackoverflow.com/questions/50390257/apps-get-models-only-custom-models – Thinker May 17 '18 at 12:13
2

apps.get_models() will return all installed models, if you want to restrict the set of installed apps used by get_app_config[s] you can use set_available_apps:

from django.apps import apps
myapp = apps.set_available_apps(list_of_available_apps)
Dhia
  • 10,119
  • 11
  • 58
  • 69
1

See this SO post.

apps.get_models() will return all installed models. If you are looking for a list of models for a specific app, do the following:

from django.apps import apps
myapp = apps.get_app_config('myapp')
myapp.models #returns an OrderedDict

Also, for reference, here's the source of get_models() to see how it works

Anupam
  • 14,950
  • 19
  • 67
  • 94