Code

Opened 5 months ago

Closed 5 months ago

Last modified 5 months 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!

Attachments (0)

Change History (3)

comment:1 Changed 5 months ago by mamalos

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Type changed from Uncategorized to Bug

comment:2 Changed 5 months ago by akaariai

  • Resolution set to invalid
  • Status changed from new to closed

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 Changed 5 months ago by mamalos

Thanks for the clarification.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.