﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32833	ContentType.objects.get_for_models() in migrations does not works for multiple models	HMaker	Sarah Boyce	"I am trying to use migrations to create default groups, I tried to run the following procedure with `migrations.RunPython()`

{{{
def create_normal_users_group(apps, *args):
    auth = SimpleNamespace(**apps.all_models['auth'])
    myapp = SimpleNamespace(**apps.all_models['myapp'])
    ContentType = apps.get_model('contenttypes', 'ContentType')
    group = auth.group.objects.create(name='Normal Users')
    contenttypes = ContentType.objects.get_for_models(myapp.user, myapp.proxy) # there are more models...
   # ...
}}}
but it raises `AttributeError`

{{{
  File ""/home/user/projects/myapp/.venv/lib/python3.8/site-packages/django/contrib/contenttypes/models.py"", line 89, in get_for_models
    opts_models = needed_opts.pop(ct.model_class()._meta, [])
AttributeError: 'ContentType' object has no attribute 'model_class'
}}}
it works when I pass a single model to `ContentType.objects.get_for_models()`, but `get_for_models()` is supposed to work with multiple models.

EDIT:
Adding `model_class()` to `ContentType` makes it work

{{{
def create_normal_users_group(apps, *args):
    auth = SimpleNamespace(**apps.all_models['auth'])
    myapp = SimpleNamespace(**apps.all_models['myapp'])
    ContentType = apps.get_model('contenttypes', 'ContentType')
    # this makes it work =========================
    def model_class(self):
        return apps.get_model(self.app_label, self.model)
    ContentType.model_class = model_class
    # ========================================
    group = auth.group.objects.create(name='Normal Users')
    contenttypes = ContentType.objects.get_for_models(myapp.user, myapp.proxy) # there are more models...
   # ...
}}}
"	Bug	closed	contrib.contenttypes	3.1	Normal	fixed			Ready for checkin	1	0	0	0	0	0
