I am running this code on django 1.5.1:
{{{
low_afpr = Result2.objects.filter(metric='afpr', value__lte=0.01,
window_size__gte=1000)
high_tpr_low_afpr = []
for x in low_afpr_list:
if Result2.objects.filter(alg=x.alg, metric='all_commands',
nmixtures=x.nmixtures, srv_name=x.srv_name, train_size=x.train_size,
gtest_size=x.gtest_size, btest_size=x.btest_size,
window_size=x.window_size, num_clusters=x.num_clusters,
value__gte=0.9).count() > 0:
high_tpr_low_afpr.append(Result2.objects.filter(alg=x.alg,
metric='all_commands', nmixtures=x.nmixtures, srv_name=x.srv_name,
train_size=x.train_size, gtest_size=x.gtest_size, btest_size=x.btest_size,
window_size=x.window_size, num_clusters=x.num_clusters, value__gte=0.9) |
Result2.objects.get(id=x.id))
}}}
and get an error of "AttributeError: 'Result2' object has no attribute
'_known_related_objects'" with the following traceback:
{{{
<ipython-input-88-ab5daa381b14> in <module>()
2 for x in low_afpr_list:
3 if Result2.objects.filter(alg=x.alg, metric='all_commands',
nmixtures=x.nmixtures, srv_name=x.srv_name, train_size=x.train_size,
gtest_size=x.gtest_size, btest_size=x.btest_size,
window_size=x.window_size, num_clusters=x.num_clusters,
value__gte=0.9).count() > 0:
----> 4 high_tpr_low_afpr.append(Result2.objects.filter(alg=x.alg,
metric='all_commands', nmixtures=x.nmixtures, srv_name=x.srv_name,
train_size=x.train_size, gtest_size=x.gtest_size, btest_size=x.btest_size,
window_size=x.window_size, num_clusters=x.num_clusters, value__gte=0.9) |
Result2.objects.get(id=x.id))
/usr/lib/python2.7/site-packages/django/db/models/query.py in __or__(self,
other)
231 if isinstance(other, EmptyQuerySet):
232 return combined
--> 233 combined._merge_known_related_objects(other)
234 combined.query.combine(other.query, sql.OR)
235 return combined
/usr/lib/python2.7/site-packages/django/db/models/query.py in
_merge_known_related_objects(self, other)
955 Keep track of all known related objects from either
QuerySet instance.
956 """
--> 957 for field, objects in
other._known_related_objects.items():
958 self._known_related_objects.setdefault(field,
{}).update(objects)
959
AttributeError: 'Result2' object has no attribute '_known_related_objects'
}}}
If I change my code so that the last line uses filter() instead of get,
ie:
{{{
high_tpr_low_afpr.append(Result2.objects.filter(alg=x.alg,
metric='all_commands', nmixtures=x.nmixtures, srv_name=x.srv_name,
train_size=x.train_size, gtest_size=x.gtest_size, btest_size=x.btest_size,
window_size=x.window_size, num_clusters=x.num_clusters, value__gte=0.9) |
Result2.objects.filter(id=x.id))
}}}
I get the result I was hoping for in the first place.
I assume that concatenating result sets with bitwise or operator should
work regardless of the way the ResultSet object has been created (whether
we used get(), or filter()), hence I started this issue.
Thank you for your attention and keep up the good work!
--
Ticket URL: <https://code.djangoproject.com/ticket/21436>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* type: Uncategorized => Bug
* needs_tests: => 0
* needs_docs: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/21436#comment:1>
* status: new => closed
* resolution: => invalid
Comment:
The .get() method returns an instance, but queryset combining with | or &
works only for querysets. So, use .filter(id=x.id) instead of .get().
--
Ticket URL: <https://code.djangoproject.com/ticket/21436#comment:2>
Comment (by mamalos):
Thanks for the clarification.
--
Ticket URL: <https://code.djangoproject.com/ticket/21436#comment:3>