#32833: ContentType.objects.get_for_models() in migrations does not works for
multiple models
-------------------------------------+-------------------------------------
     Reporter:  Heraldo Lucena       |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:                       |                  Version:  3.1
  contrib.contenttypes               |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Heraldo Lucena:

Old description:

> 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.

New description:

 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...
    # ...
 }}}

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32833#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.8e88685b2b65ba5030865e96a6f074bd%40djangoproject.com.

Reply via email to