Convert a list to queryset and override the one in modelAdmin

4,981 views
Skip to first unread message

Vinicius Massuchetto

unread,
Feb 21, 2011, 12:47:42 PM2/21/11
to Django Users
Hi.

I know that's not exactly the proper way of building a queryset, but I
managed to build a list of objects of a model across operations and
annotations, and I want to show that an admin change list.

I'm currently declaring a queryset() method in modelAdmin, and
building this list there, but it fails to a "database error" admin
screen if I return the list.

Is there something I can use to convert it?

Thanks.
--
Vinicius Massuchetto

Daniel Roseman

unread,
Feb 21, 2011, 3:56:17 PM2/21/11
to django...@googlegroups.com
You can't "convert" a list to queryset, as a queryset is - as the name implies - a database query.

What you could do is get all the IDs from the list and pass that into a new query:

    my_queryset = MyModel.objects.filter(id__in=[o.id for o in my_list])
--
DR. 

Vinicius Massuchetto

unread,
Feb 21, 2011, 7:47:40 PM2/21/11
to django...@googlegroups.com
2011/2/21 Daniel Roseman <dan...@roseman.org.uk>:

> On Monday, February 21, 2011 5:47:42 PM UTC, Vinicius Massuchetto wrote:

> You can't "convert" a list to queryset, as a queryset is - as the name
> implies - a database query.

I imagined that. =/

> What you could do is get all the IDs from the list and pass that into a new
> query:
>     my_queryset = MyModel.objects.filter(id__in=[o.id for o in my_list])

I tried that, and that's the problem.

The objects I want don't really "exist", as my base data is the output
for `SomeModel.objects.annotate(n = Count(n)).filter(n = 0)` of a
ForeignKey of the model that I want to have the queryset overrided.

I'm confused how to deal with that.
Hope I made myself clear.

Thanks.
--
Vinicius Massuchetto

Vinicius Massuchetto

unread,
Feb 21, 2011, 8:17:24 PM2/21/11
to django...@googlegroups.com
2011/2/21 Vinicius Massuchetto <viniciusm...@gmail.com>:

> 2011/2/21 Daniel Roseman <dan...@roseman.org.uk>:
>> On Monday, February 21, 2011 5:47:42 PM UTC, Vinicius Massuchetto wrote:
>
>> You can't "convert" a list to queryset, as a queryset is - as the name
>> implies - a database query.
>
> I imagined that. =/
>
>> What you could do is get all the IDs from the list and pass that into a new
>> query:
>>     my_queryset = MyModel.objects.filter(id__in=[o.id for o in my_list])
>
> I tried that, and that's the problem.
>
> The objects I want don't really "exist", as my base data is the output
> for `SomeModel.objects.annotate(n = Count(n)).filter(n = 0)` of a
> ForeignKey of the model that I want to have the queryset overrided.

As I think things are amazingly confusing here, here's a detailed
description of what I'm trying to do:

http://stackoverflow.com/questions/5056362/django-admin-change-list-to-display-null-foreignkeys-backwards

Thanks for helping.
--
Vinicius Massuchetto
http://vinicius.soylocoporti.org.br

Reply all
Reply to author
Forward
0 new messages