In Django is there a way to use FOO_set on a model with a foreign key that refers to objects of the same model type?

As the error message says, you're trying to filter a queryset that has already been sliced.

Up vote 0 down vote favorite share g+ share fb share tw.

I have a model like this: class Video(models. Model): views = models. ManyToManyField(User, null=True, related_name='views') parent = models.

ForeignKey('self', null=True) def __unicode__(self): return self. Title def _get_views_count(self): return self.views.count() def _get_comments_count(self): return self. Comment_set.all().count() def _get_playlists(self): return self.

Playlist_set.all() views_count = property(_get_views_count) comments_count = property(_get_comments_count) playlists = property(_get_playlists) most_viewed_objects = MostViewedVideoManager() objects = models.Manager() class MostViewedVideoManager(models. Manager): def get_query_set(self): return super(MostViewedProjectManager, self). Get_query_set().

Annotate(lovers=Count('views')). Order_by('viewers'):20 And I want to get the set of Videos that have a given Parent. I thought I could just do something like this: v = Video.objects.

Get(id=3) v. Video_set.all() However it's throwing this error: AssertionError: Cannot filter a query once a slice has been taken. (full trace) /usr/local/lib/python2.7/dist-packages/django/db/models/manager.

Pyc in all(self) 115 116 def all(self): --> 117 return self. Get_query_set() 118 119 def count(self): /usr/local/lib/python2.7/dist-packages/django/db/models/fields/related. Pyc in get_query_set(self) 421 def get_query_set(self): 422 db = self.

_db or router. Db_for_read(rel_model, instance=instance) --> 423 return superclass. Get_query_set(self).

Using(db). Filter(**(self. Core_filters)) 424 425 def add(self, *objs): /usr/local/lib/python2.7/dist-packages/django/db/models/query.

Pyc in filter(self, *args, **kwargs) 548 set. 549 """ --> 550 return self. _filter_or_exclude(False, *args, **kwargs) 551 552 def exclude(self, *args, **kwargs): /usr/local/lib/python2.7/dist-packages/django/db/models/query.

Pyc in _filter_or_exclude(self, negate, *args, **kwargs) 560 if args or kwargs: 561 assert self.query. Can_filter(), \ --> 562 "Cannot filter a query once a slice has been taken. " 563 564 clone = self.

_clone() I believe it's related to the MostViewedManager - since taking that out resolves the issue. What's the correct way to do this? Django django-models django-queryset django-queries django-query link|improve this question edited Nov 2 '11 at 15:26 asked Nov 2 '11 at 14:339-bits3348 52% accept rate.

That error is not coming from that code. Please post the full code, and the real traceback. Plus, please accept some answers.

– Daniel Roseman Nov 2 '11 at 14:44 Thanks - you're right - it seems like the error is related to the manager class MostViewedVideoManager....but I'm not sure why...I've added a full trace and more code. – 9-bits Nov 2 '11 at 15:27 You're somewhere trying to filter a queryset that is returned from the MostViewedVideoManager... – lazerscience Nov 2 '11 at 15:42 Ok - I found that reordering the managers in my model solved the problem ...though I'm not sure why. Ie.

Objects = models.Manger() before most_viewed_objects = MostViewedVideoManager() – 9-bits Nov 2 '11 at 15:50 1 The first manager listed becomes the default manager. In your case, that was your custom manager that returned the sliced queryset. When you switch them, the default became the standard manager which doesn't slice its queryset.

– Chris Pratt Nov 2 '11 at 15:58.

As the error message says, you're trying to filter a queryset that has already been sliced. Don't return a sliced queryset from a manager's get_query_set method. Either return it unsliced, or create a separate method that returns the sliced version.

Ah that makes sense - Thanks! – 9-bits Nov 2 '11 at 16:02.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions