[Django] #21436: 1.5.1 returns "object has no attribute '_known_related_objects'" when using the bitwise or operator with objects.get()

44 views
Skip to first unread message

Django

unread,
Nov 14, 2013, 7:15:34 AM11/14/13
to django-...@googlegroups.com
#21436: 1.5.1 returns "object has no attribute '_known_related_objects'" when using
the bitwise or operator with objects.get()
-------------------------------------+-------------------------------------
Reporter: mamalos | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 1.5
(models, ORM) | Keywords: bitwise or, model, get,
Severity: Normal | filter
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Hi all,

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.

Django

unread,
Nov 14, 2013, 7:20:14 AM11/14/13
to django-...@googlegroups.com
#21436: 1.5.1 returns "object has no attribute '_known_related_objects'" when using
the bitwise or operator with objects.get()
-------------------------------------+-------------------------------------
Reporter: mamalos | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: bitwise or, model, | Unreviewed
get, filter | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by mamalos):

* needs_better_patch: => 0
* type: Uncategorized => Bug
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/21436#comment:1>

Django

unread,
Nov 14, 2013, 7:31:32 AM11/14/13
to django-...@googlegroups.com
#21436: 1.5.1 returns "object has no attribute '_known_related_objects'" when using
the bitwise or operator with objects.get()
-------------------------------------+-------------------------------------
Reporter: mamalos | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.5
(models, ORM) | Resolution: invalid

Severity: Normal | Triage Stage:
Keywords: bitwise or, model, | Unreviewed
get, filter | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by akaariai):

* 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>

Django

unread,
Nov 14, 2013, 8:48:35 AM11/14/13
to django-...@googlegroups.com
#21436: 1.5.1 returns "object has no attribute '_known_related_objects'" when using
the bitwise or operator with objects.get()
-------------------------------------+-------------------------------------
Reporter: mamalos | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.5
(models, ORM) | Resolution: invalid
Severity: Normal | Triage Stage:
Keywords: bitwise or, model, | Unreviewed
get, filter | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by mamalos):

Thanks for the clarification.

--
Ticket URL: <https://code.djangoproject.com/ticket/21436#comment:3>

Reply all
Reply to author
Forward
0 new messages