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.
On Tuesday, June 17, 2014 8:18:44 PM UTC-5, Tim Graham wrote: > > Please use django-users for usage questions. This list is for the > discussion of the development of Django itself. > > On Tuesday, June 17, 2014 7:41:04 PM UTC-4, Jason Skicewicz wrote: >> >> 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 >> > -- You received this message because you are subscribed to the Google Groups "Django developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To post to this group, send email to django-developers@googlegroups.com. Visit this group at http://groups.google.com/group/django-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/ce31c2fd-2597-4cdf-872e-e4c1eab9fbc2%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.