﻿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
33207	django.apps.apps.get_model() does not allow namespaced (PEP 420) apps to get a model	Jonas L.	nobody	"The '''django.contrib.auth''' app use '''django.apps.apps.get_model''' to load the '''AUTH_USER_MODEL''' from the settings. While doing so it leverage the shortcut to provide the ''app_name.ModelName'''.

The shortcut does not take into count that app_name can be a namespaced (PEP 420) app like '''my_namespace.my_app''', and that we might want to load a model from '''my_namespace.my_app.User'''.

This result in the following error:
{{{
Traceback (most recent call last):
  File ""/usr/local/lib/python3.7/dist-packages/django/db/models/utils.py"", line 15, in make_model_tuple
    app_label, model_name = model.split(""."")
ValueError: too many values to unpack (expected 2)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ""/usr/local/bin/libretime-api"", line 10, in <module>
    sys.exit(main())
  File ""/usr/local/lib/python3.7/dist-packages/libretime/api/cli.py"", line 18, in main
    execute_from_command_line(sys.argv)
  File ""/usr/local/lib/python3.7/dist-packages/django/core/management/__init__.py"", line 419, in execute_from_command_line
    utility.execute()
  File ""/usr/local/lib/python3.7/dist-packages/django/core/management/__init__.py"", line 395, in execute
    django.setup()
  File ""/usr/local/lib/python3.7/dist-packages/django/__init__.py"", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File ""/usr/local/lib/python3.7/dist-packages/django/apps/registry.py"", line 114, in populate
    app_config.import_models()
  File ""/usr/local/lib/python3.7/dist-packages/django/apps/config.py"", line 301, in import_models
    self.models_module = import_module(models_module_name)
  File ""/usr/lib/python3.7/importlib/__init__.py"", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File ""<frozen importlib._bootstrap>"", line 1006, in _gcd_import
  File ""<frozen importlib._bootstrap>"", line 983, in _find_and_load
  File ""<frozen importlib._bootstrap>"", line 967, in _find_and_load_unlocked
  File ""<frozen importlib._bootstrap>"", line 677, in _load_unlocked
  File ""<frozen importlib._bootstrap_external>"", line 728, in exec_module
  File ""<frozen importlib._bootstrap>"", line 219, in _call_with_frames_removed
  File ""/usr/local/lib/python3.7/dist-packages/django/contrib/admin/models.py"", line 39, in <module>
    class LogEntry(models.Model):
  File ""/usr/local/lib/python3.7/dist-packages/django/db/models/base.py"", line 161, in __new__
    new_class.add_to_class(obj_name, obj)
  File ""/usr/local/lib/python3.7/dist-packages/django/db/models/base.py"", line 326, in add_to_class
    value.contribute_to_class(cls, name)
  File ""/usr/local/lib/python3.7/dist-packages/django/db/models/fields/related.py"", line 747, in contribute_to_class
    super().contribute_to_class(cls, name, private_only=private_only, **kwargs)
  File ""/usr/local/lib/python3.7/dist-packages/django/db/models/fields/related.py"", line 318, in contribute_to_class
    lazy_related_operation(resolve_related_class, cls, self.remote_field.model, field=self)
  File ""/usr/local/lib/python3.7/dist-packages/django/db/models/fields/related.py"", line 80, in lazy_related_operation
    return apps.lazy_model_operation(partial(function, **kwargs), *model_keys)
  File ""/usr/local/lib/python3.7/dist-packages/django/db/models/fields/related.py"", line 78, in <genexpr>
    model_keys = (make_model_tuple(m) for m in models)
  File ""/usr/local/lib/python3.7/dist-packages/django/db/models/utils.py"", line 24, in make_model_tuple
    ""must be of the form 'app_label.ModelName'."" % model
ValueError: Invalid model reference 'libretime.api.User'. String model references must be of the form 'app_label.ModelName'.

}}}

Some links:
https://github.com/django/django/blob/e2f778d57947d168a875159e6df075255eea4bbc/django/contrib/auth/__init__.py#L160
https://docs.djangoproject.com/en/3.2/ref/applications/#django.apps.apps.get_model"	Bug	closed	Core (Other)	3.2	Normal	invalid			Unreviewed	0	0	0	0	0	0
