Opened 9 years ago

Closed 9 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


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 "", line 10, in <module>
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/core/management/", line 427, in execute_from_command_line
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/core/management/", line 391, in execute
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/", line 21, in setup
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/apps/", line 105, in populate
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/apps/", line 160, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/", line 37, in import_module
  File "/home/timgraham/code/mysite/polls/", line 21, in <module>
    class Choice(models.Model):
  File "/home/timgraham/code/mysite/polls/", 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/", 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/", 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/", 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/", 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/", line 101, in wrapper
    result = user_function(*args, **kwds)
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/apps/", line 155, in get_models
  File "/home/timgraham/code/tutorial/local/lib/python2.7/site-packages/django/apps/", 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 Changed 9 years ago by Anssi Kääriäinen

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 Changed 9 years ago by Tim Graham

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 Changed 9 years ago by Marc Tamlyn

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