Strange behavior when running a join in Django shell versus script

27 views
Skip to first unread message

Jason Skicewicz

unread,
Jun 18, 2014, 2:16:23 AM6/18/14
to django...@googlegroups.com
I have a many to many relationship from my UserProfile object to an object called ThemeTimes.  The declaration looks like the following:

   theme_times = models.ManyToManyField('fixupthemes.FixupThemeTime', blank=True,
        related_name='profiles', editable=False)

the FixupThemeTime model, has a field called timestamp that is declared as follows:

    timestamp = models.DateTimeField(null=False, blank=False)

In the Django shell or the view code, the following works just fine:

    users = UserProfile.objects.filter(theme_times__timestamp__gt=datetime.now()).distinct()

Which returns me all users that have theme times in the future (availability).  When I run this same query in a simple script shown here:

import os
import sys
sys.path = [ '/usr/local/projectfixup', ] + sys.path
os.environ['DJANGO_SETTINGS_MODULE'] = 'projectfixup.settings'
from datetime import datetime
from projectfixup.accounts.models import UserProfile

if __name__ == "__main__":
    users = UserProfile.objects.filter(theme_times__timestamp__gt=datetime.now()).distinct()
    print users.count()
    sys.exit(0)

It results in a traceback with the following message:

Traceback (most recent call last):
  File "test_script.py", line 13, in <module>
    users = UserProfile.objects.filter(theme_times__timestamp__gt=datetime.now()).distinct()
  File "/home/jason/venv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in filter
    return self.get_query_set().filter(*args, **kwargs)
  File "/home/jason/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 621, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/home/jason/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 639, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/home/jason/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1250, in add_q
    can_reuse=used_aliases, force_having=force_having)
  File "/home/jason/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1072, in add_filter
    lookup_field = lookup_model._meta.get_field(field_name)
AttributeError: 'str' object has no attribute '_meta'

I'm at a loss as to why this is happening, but through debugging, when Django attempts to get the field via script, lookup_model is a string whereas in the view code, it's a model instance.  Not sure if this is a known problem or not, and my solution to date has been to make two separate queries in scripts instead of doing the join.  But the inconsistency is driving me a bit mad.

Any ideas on a resolution to the problem?

-Jason

Daniel Roseman

unread,
Jun 18, 2014, 5:07:56 AM6/18/14
to django...@googlegroups.com
You appear to be using an old version of Django - line 1072 in sql/query.py hasn't been what shows in the traceback since version 1.4. As a first suggestion, you should upgrade to see if it solves your problem.

Otherwise, you may need to post your model and manager so we can see more of what's going on.
--
DR.
Reply all
Reply to author
Forward
0 new messages