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 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 [email protected].
To post to this group, send email to [email protected].
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/9f491a4e-8cbd-4990-b0e2-d214be9ea220%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.