Strange behavior when running simple query via script versus view code

90 views
Skip to first unread message

Jason Skicewicz

unread,
Jun 17, 2014, 7:41:04 PM6/17/14
to django-d...@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 view code, the following works 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

Tim Graham

unread,
Jun 17, 2014, 9:18:44 PM6/17/14
to django-d...@googlegroups.com
Please use django-users for usage questions. This list is for the discussion of the development of Django itself.

Jason Skicewicz

unread,
Jun 18, 2014, 2:12:15 AM6/18/14
to django-d...@googlegroups.com
I've been using Django since 0.96, and I don't consider this to be a usage problem, but more like a possible bug.  I've never seen different behavior in a script versus view / shell code like this, so the point was to ask the developers if anyone has any insight on what could possibly be going on here.  Many of my projects have cron jobs that run scripts.  Running this query in Django shell works just fine.  Running it in the view code works just fine too.  Running it in a script doesn't.

Shai Berger

unread,
Jun 18, 2014, 3:48:59 AM6/18/14
to django-d...@googlegroups.com
Hi,

On Tuesday 17 June 2014 16:41:04 Jason Skicewicz wrote:
> >
> > 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?
>
I'm not sure -- but it may be that your 'fixupthemes.FixupThemeTime' model is
not loaded in the script. Try adding an explicit import of it before the
query.

If this works, then:

a) The issue has been resolved in 1.7 by the app-load refactor;
b) This does belong on django-users.

HTH,
Shai.

Aymeric Augustin

unread,
Jun 18, 2014, 6:16:55 AM6/18/14
to django-d...@googlegroups.com
2014-06-18 9:48 GMT+02:00 Shai Berger <sh...@platonix.com>:
I'm not sure -- but it may be that your 'fixupthemes.FixupThemeTime' model is
not loaded in the script. Try adding an explicit import of it before the
query.

If this works, then:

a) The issue has been resolved in 1.7 by the app-load refactor;
b) This does belong on django-users.

I'm pretty sure (a) is the answer.

That's exactly the reason why I added the requirement that standalone scripts call `django.setup()` before doing anything with the ORM.
 
--
Aymeric.
Reply all
Reply to author
Forward
0 new messages