Opened 11 years ago

Closed 11 years ago

#21828 closed Bug (wontfix)

Can no longer use model meta methods in other methods after app loading changes

Reported by: Tim Graham Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: app-loading
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

To reproduce, add the following as an attribute to the Choice model in the tutorial and try to runserver:

foo = Poll._meta.get_all_field_names()

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 427, in execute_from_command_line
    utility.execute()
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 391, in execute
    django.setup()
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/apps/registry.py", line 105, in populate
    app_config.import_models(all_models)
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/apps/base.py", line 160, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/timgraham/code/mysite/polls/models.py", line 21, in <module>
    class Choice(models.Model):
  File "/home/timgraham/code/mysite/polls/models.py", line 26, in Choice
    foo = Poll._meta.get_all_field_names()
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/db/models/options.py", line 420, in get_all_field_names
    cache = self.init_name_map()
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/db/models/options.py", line 433, in init_name_map
    for f, model in self.get_all_related_m2m_objects_with_model():
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/db/models/options.py", line 550, in get_all_related_m2m_objects_with_model
    cache = self._fill_related_many_to_many_cache()
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/db/models/options.py", line 564, in _fill_related_many_to_many_cache
    for klass in self.apps.get_models():
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
    result = user_function(*args, **kwds)
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/apps/registry.py", line 155, in get_models
    self.check_ready()
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/apps/registry.py", line 118, in check_ready
    raise RuntimeError("App registry isn't ready yet.")
RuntimeError: App registry isn't ready yet.

Change History (3)

comment:1 by Anssi Kääriäinen, 11 years ago

The get_all_field_names() method isn't safe to call when all apps aren't loaded. For example all reverse m2m fields should be in get_all_field_names(), but all reverse m2m fields aren't guaranteed to be available until all models are loaded (any model not yet loaded could have a m2m field pointing to current model).

Based on the report it is hard to decide how to proceed. Which use cases are broken? Are there other ways to achieve those use cases?

comment:2 by Tim Graham, 11 years ago

The use case is attributes on a model that includes list of fields from mixins. I can replace get_all_field_names() with local_fields to achieve what I'm trying to do. If we can't and/or don't want to make any code changes, we can close this. Since _meta is private API, I don't think it warrants a documentation note.

comment:3 by Marc Tamlyn, 11 years ago

Resolution: wontfix
Status: newclosed

I agree with akaariai's assessment of the situation. get_all_field_names() should fail in this situation. Consequently, closing as wontfix.

Note: See TracTickets for help on using tickets.
Back to Top