Opened 11 years ago

Closed 11 years ago

#21178 closed Uncategorized (needsinfo)

Site.objects.get_current() causes circular import while executing collectstatic

Reported by: james@… Owned by: nobody
Component: Uncategorized Version: 1.5
Severity: Normal Keywords: site, circular reference, circular import
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This app ​https://bitbucket.org/jameslin/zencoder-public which being used by 2 other apps. Typical references look like this:
from zencode.signals import zencode
post_save.connect(zencode, Video, dispatch_uid="videos.models.video")
Class Video(models.Model):
videos = generic.GenericRelation('zencode.ZencodedVideo', related_name="zen_videos")
Everything works fine on developmetn but when I deploy to stable, I get problems like cannot import zencode.
Normally it would still work on stable if I only have 1 app that uses the zencode app, but since there are 2 apps in this case, it's causing some circular import

Traceback (most recent call last):
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/core/management/base.py", line 222, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/core/management/base.py", line 255, in execute
    output = self.handle(*args, **options)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/core/management/base.py", line 385, in handle
    return self.handle_noargs(**options)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 164, in handle_noargs
    collected = self.collect()
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
    handler(path, prefixed_path, storage)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 285, in copy_file
    if not self.delete_file(path, prefixed_path, source_storage):
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 224, in delete_file
    source_last_modified = source_storage.modified_time(path)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/core/files/storage.py", line 277, in modified_time
    return datetime.fromtimestamp(os.path.getmtime(self.path(name)))
  File "/srv/www/weddings/local/lib/python2.7/site-packages/dajaxice/finders.py", line 55, in path
    path = self.get_or_create_file(name)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/dajaxice/finders.py", line 21, in get_or_create_file
    data = getattr(self, self.files[path])()
  File "/srv/www/weddings/local/lib/python2.7/site-packages/dajaxice/finders.py", line 68, in dajaxice_core_js
    dajaxice_autodiscover()
  File "/srv/www/weddings/local/lib/python2.7/site-packages/dajaxice/core/Dajaxice.py", line 135, in dajaxice_autodiscover
    import_module("%s.ajax" % app)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/srv/www/weddings/weddings/directory/ajax.py", line 3, in <module>
    from views.index import filter_content
  File "/srv/www/weddings/weddings/directory/views/index.py", line 4, in <module>
    from directory.models import *
  File "/srv/www/weddings/weddings/directory/models.py", line 26, in <module>
    from zencode.signals import zencode
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django_mash_zencode-0.1-py2.7.egg/zencode/signals.py", line 6, in <module>
    site = Site.objects.get_current()
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/contrib/sites/models.py", line 26, in get_current
    current_site = self.get(pk=sid)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/query.py", line 391, in get
    clone = self.filter(*args, **kwargs)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/query.py", line 667, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/query.py", line 685, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1259, in add_q
    can_reuse=used_aliases, force_having=force_having)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1127, in add_filter
    process_extras=process_extras)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1313, in setup_joins
    field, model, direct, m2m = opts.get_field_by_name(name)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/options.py", line 351, in get_field_by_name
    cache = self.init_name_map()
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/options.py", line 380, in init_name_map
    for f, model in self.get_all_related_m2m_objects_with_model():
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/options.py", line 469, in get_all_related_m2m_objects_with_model
    cache = self._fill_related_many_to_many_cache()
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/options.py", line 483, in _fill_related_many_to_many_cache
    for klass in get_models(only_installed=False):
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/loading.py", line 197, in get_models
    self._populate()
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/loading.py", line 75, in _populate
    self.load_app(app_name)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/loading.py", line 96, in load_app
    models = import_module('.models', app_name)
  File "/srv/www/weddings/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/srv/www/weddings/weddings/videos/models.py", line 8, in <module>
    from zencode.signals import zencode
ImportError: cannot import name zencode

Change History (3)

comment:1 by Daniele Procida, 11 years ago

Why do you think this is not a circular import problem between two of your modules?

comment:2 by Daniele Procida, 11 years ago

It's quite common for Python loading to be affected by Django settings such as DEBUG. It's possible that your applications have a circular import, that DEBUG = True allows you to escape from.

What would be necessary to demonstrate that this really is an issue in Django would be a minimal test project (settings and other required files) in which the two applications are both installed, so that the problem can be independently verified and investigated.

comment:3 by Tim Graham, 11 years ago

Resolution: needsinfo
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top