Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#21436 closed Bug (invalid)

1.5.1 returns "object has no attribute '_known_related_objects'" when using the bitwise or operator with objects.get()

Reported by: mamalos Owned by: nobody
Component: Database layer (models, ORM) Version: 1.5
Severity: Normal Keywords: bitwise or, model, get, filter
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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!

Change History (3)

comment:1 by mamalos, 11 years ago

Type: UncategorizedBug

comment:2 by Anssi Kääriäinen, 11 years ago

Resolution: invalid
Status: newclosed

The .get() method returns an instance, but queryset combining with | or & works only for querysets. So, use .filter(id=x.id) instead of .get().

comment:3 by mamalos, 11 years ago

Thanks for the clarification.

Note: See TracTickets for help on using tickets.
Back to Top