#33207: django.apps.apps.get_model shortcut does not allow namespaced (PEP 420)
apps to get a model
-----------------------------------------+------------------------
Reporter: Jonas L. | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 3.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
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
--
Ticket URL: <https://code.djangoproject.com/ticket/33207>
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/049.b7ed44ed2f8e2142d3b10b748c8c250f%40djangoproject.com.