Opened 13 years ago

Closed 11 years ago

#17667 closed Bug (fixed)

App loading skips erroneous applications after first try

Reported by: ea2100@… Owned by: Blaker0111
Component: Database layer (models, ORM) Version: 1.3
Severity: Normal Keywords: model
Cc: anssi.kaariainen@…, Łukasz Rekucki Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

if you guys could look through this trackback, i would really appreciate it (pasted below).

from it you can see that i can 'see' the items in the model, however, when trying to do an .order_by(), i get an ImportError BUT if i run the .order_by() command two more times, it works!?

could this be a Django bug?

the trackback is from a python console execution

my end goal is to have that working in a script file, seperate from django

(.env)user@server [~/python/scripts]# python
Python 2.6.4 (r264:75706, Nov  6 2011, 21:07:26)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from django.core.management import setup_environ
>>> from django.utils import simplejson
>>> import examplecom.settings
>>> setup_environ(examplecom.settings)
'/home/user/.env/lib/python2.6/site-packages/examplecom'
>>> from examplecom.main.models import Activity
>>> Activity.objects.all()
[<Activity: Test>, <Activity: Test2>]
>>> Activity.objects.order_by('-created')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/query.py", line 69, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/query.py", line 84, in __len__
    self._result_cache.extend(self._iter)
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/query.py", line 273, in iterator
    for row in compiler.results_iter():
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 680, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 725, in execute_sql
    sql, params = self.as_sql()
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 60, in as_sql
    ordering, ordering_group_by = self.get_ordering()
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 349, in get_ordering
    self.query.model._meta, default_order=asc):
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 378, in find_ordering_name
    opts, alias, False)
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/sql/query.py", line 1248, in setup_joins
    field, model, direct, m2m = opts.get_field_by_name(name)
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/options.py", line 307, in get_field_by_name
    cache = self.init_name_map()
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/options.py", line 337, in init_name_map
    for f, model in self.get_all_related_m2m_objects_with_model():
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/options.py", line 414, in get_all_related_m2m_objects_with_model
    cache = self._fill_related_many_to_many_cache()
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/options.py", line 428, in _fill_related_many_to_many_cache
    for klass in get_models():
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/loading.py", line 167, in get_models
    self._populate()
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/loading.py", line 61, in _populate
    self.load_app(app_name, True)
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/loading.py", line 76, in load_app
    app_module = import_module(app_name)
  File "/home/user/.env/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
ImportError: No module named main
>>> Activity.objects.order_by('-created')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/query.py", line 69, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/query.py", line 84, in __len__
    self._result_cache.extend(self._iter)
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/query.py", line 273, in iterator
    for row in compiler.results_iter():
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 680, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 725, in execute_sql
    sql, params = self.as_sql()
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 60, in as_sql
    ordering, ordering_group_by = self.get_ordering()
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 349, in get_ordering
    self.query.model._meta, default_order=asc):
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 378, in find_ordering_name
    opts, alias, False)
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/sql/query.py", line 1248, in setup_joins
    field, model, direct, m2m = opts.get_field_by_name(name)
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/options.py", line 307, in get_field_by_name
    cache = self.init_name_map()
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/options.py", line 337, in init_name_map
    for f, model in self.get_all_related_m2m_objects_with_model():
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/options.py", line 414, in get_all_related_m2m_objects_with_model
    cache = self._fill_related_many_to_many_cache()
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/options.py", line 428, in _fill_related_many_to_many_cache
    for klass in get_models():
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/loading.py", line 167, in get_models
    self._populate()
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/loading.py", line 61, in _populate
    self.load_app(app_name, True)
  File "/home/user/.env/lib/python2.6/site-packages/django/db/models/loading.py", line 76, in load_app
    app_module = import_module(app_name)
  File "/home/user/.env/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
ImportError: No module named blog
>>> Activity.objects.order_by('-created')
[<Activity: Test2>, <Activity: Test>]

Attachments (1)

17667.diff (2.6 KB ) - added by Anssi Kääriäinen 13 years ago.

Download all attachments as: .zip

Change History (5)

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

Cc: anssi.kaariainen@… added
Has patch: set
Summary: Inconsistant modelApp loading skips erroneous applications after first try
Triage Stage: UnreviewedAccepted

Yes, it looks like there is a bug in Django. You should get the first error consistently, it should not go away with repeating the operation. This is an issue with app loading, not order_by.

The problem seems to be in load_app() of loading.py:

        self.handled[app_name] = None
        self.nesting_level += 1
        app_module = import_module(app_name)

This causes the error to be skipped on subsequent loads. Putting the import first will fix the problem. In the attached patch this is fixed. The borg-pattern of AppCache caused some testing problems, but I hope what I did is OK.

For the reporter: you most likely have a couple of references to non-existing applications in your settings.INSTALLED_APPS. This causes the import errors for you. As said, the bug is that the errors should repeat.

by Anssi Kääriäinen, 13 years ago

Attachment: 17667.diff added

comment:2 by ea2100@…, 13 years ago

I was able to get this working just a few minutes ago. I needed to add "sys.path.append(<django project path>)" to the list of imports/etc.

I'm glad I was able to help in identifying a bug though! Thank you.

comment:3 by Łukasz Rekucki, 12 years ago

Cc: Łukasz Rekucki added

comment:4 by Tim Graham <timograham@…>, 11 years ago

Resolution: fixed
Status: newclosed

In 04489c7dbf8f69de84ca272a0a1710e7b6067e9d:

Fixed #17667 -- Prevented app loading from skipping nonexistent apps after the first try

Thanks ea2100@ for the report and akaariai for the patch.

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