On Wednesday, 18 June 2014 07:16:23 UTC+1, 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 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
>

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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/f8b9ed1a-31df-4d4f-8c77-cbce913284d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to