Is there some way you can keep track of the queryset? I understand it's dynamic, and maybe complex, for you to be looking for this kind of solution, but if it were possible to have a variable or dictionary keeping track of what you add to the queryset, it might be possible to use it to build the inverse.
- Paulo On Tue, Mar 23, 2010 at 1:23 PM, Vinicius Mendes | meiocodigo.com < vbmen...@gmail.com> wrote: > Ok. The code proposed by Tim Shaffer works and gives only one query. > But it makes use of subselects, what is heavy for the database. Take a > look at the generated SQL: > > 'SELECT `auth_user`.`id`, `auth_user`.`username`, > `auth_user`.`first_name`, `auth_user`.`last_name`, > `auth_user`.`email`, `auth_user`.`password`, `auth_user`.`is_staff`, > `auth_user`.`is_active`, `auth_user`.`is_superuser`, > `auth_user`.`last_login`, `auth_user`.`date_joined` FROM `auth_user` > WHERE NOT (`auth_user`.`id` IN (SELECT U0.`id` FROM `auth_user` U0 > WHERE U0.`first_name` = vinicius )) LIMIT 21' > > I was thinking of something like: > > 'SELECT `auth_user`.`id`, `auth_user`.`username`, > `auth_user`.`first_name`, `auth_user`.`last_name`, > `auth_user`.`email`, `auth_user`.`password`, `auth_user`.`is_staff`, > `auth_user`.`is_active`, `auth_user`.`is_superuser`, > `auth_user`.`last_login`, `auth_user`.`date_joined` FROM `auth_user` > WHERE NOT `auth_user`.`first_name` = vinicius LIMIT 21' > > Why hit the database if I can do this with boolean login? Why not > negate the filter condition instead of doing a subselect? > > On Mar 22, 7:15 pm, Tim Shaffer <timster...@gmail.com> wrote: > > It depends. This will only run one query, when negated_queryset is > > used. > > > > queryset = User.objects.filter(first_name='vinicius') > > negated_queryset = User.objects.exclude(id__in=queryset.values("id")) > > > > Since the first queryset is not evaluated until it's used in the > > negated_queryset (as a subquery). > > > > On Mar 22, 5:55 pm, Matt Schinckel <m...@schinckel.net> wrote: > > > > > > > > > On Mar 23, 6:17 am, Phlip <phlip2...@gmail.com> wrote: > > > > > > > Just create another queryset that excludes everything in your first > > > > > queryset: > > > > > > > negated_queryset = > User.objects.exclude(id__in=queryset.values("id")) > > > > > > QuerySets are already so easy to plug-n-play... Ain't there a way to > > > > do it without whacking the database twice? > > > > > Are you sure it hits the db twice? I seem to recall a similar case > > > where I thought I was, but since the queryset evaluation is lazy, the > > > ORM potentially has the ability to make this into a single query. > > > > > (I can't recall if when I did this sort of thing I was using > > > SQLAlchemy, but I _think_ it was pure django). > > > > > Matt. > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.