help optimizing a snippet

10 views
Skip to first unread message

Szabo, Patrick (LNG-VIE)

unread,
Dec 6, 2011, 8:42:04 AM12/6/11
to django...@googlegroups.com

Hi,

 

I’v got this piece of code:

 

def get_my_choices(gruppe):

    users = User.objects.all()

    choices_list = ()

    for user in users:

        try:

            for groupe in gruppe:

                for groupe2 in user.groups.all():

                    if groupe2 == groupe and (user.id,user) not in choices_list and groupe2.name not in ['Timesheet-Boss','TimeSheet-Manager','Projektleiter','Normal-User','Manager']:

                        choices_list += ((user.id,user),)               

        except:

            pass

    return choices_list

 

The parameter is the result of a user.groups.all() call.

This function causes over db-queries and I was wondering if anyone sees a way of optimizing that a little bit.

I was playing around with values_list() but couldn’t get it working.

 

Any ideas ?

 

regards

. . . . . . . . . . . . . . . . . . . . . . . . . .

Ing. Patrick Szabo
XSLT Developer

LexisNexis
Marxergasse 25, 1030 Wien

patric...@lexisnexis.at

Tel.: 00431 534521573

Fax: +43 (1) 534 52 - 146



Tom Evans

unread,
Dec 6, 2011, 9:19:48 AM12/6/11
to django...@googlegroups.com
On Tue, Dec 6, 2011 at 1:42 PM, Szabo, Patrick (LNG-VIE)
<patric...@lexisnexis.at> wrote:
> Hi,
>
>
>
> I’v got this piece of code:
>
>
>
> def get_my_choices(gruppe):
>
>     users = User.objects.all()
>
>     choices_list = ()
>
>     for user in users:
>
>         try:
>
>             for groupe in gruppe:
>
>                 for groupe2 in user.groups.all():
>
>                     if groupe2 == groupe and (user.id,user) not in
> choices_list and groupe2.name not in
> ['Timesheet-Boss','TimeSheet-Manager','Projektleiter','Normal-User','Manager']:
>
>                         choices_list += ((user.id,user),)
>
>         except:
>
>             pass
>
>     return choices_list
>
>
>
> The parameter is the result of a user.groups.all() call.
>
> This function causes over db-queries and I was wondering if anyone sees a
> way of optimizing that a little bit.
>
> I was playing around with values_list() but couldn’t get it working.
>

If gruppe is a queryset, then isn't this equivalent:

def get_my_choices(groups):
bad_group_names = [ 'blah', 'wibble' ]
grps = groups.exclude(name__in=bad_group_names)
qs = User.objects.filter(groups__in=grps)
return [ (user.id, user) for user in qs ]

Cheers

Tom

Felipe Morales

unread,
Dec 6, 2011, 9:24:24 AM12/6/11
to django...@googlegroups.com
Hi, 

Maybe I can't understand you, but try with

u = User.objects.filter(groups__name__in = gruppe).exclude(groups__name__in='Timesheet-Boss','TimeSheet-Manager','Projektleiter','Normal-User','Manager']).values_list('id', 'username')

2011/12/6 Szabo, Patrick (LNG-VIE) <patric...@lexisnexis.at>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Felipe Morales C.
Ingenierío de Ejecución en Computación e Informática.

Szabo, Patrick (LNG-VIE)

unread,
Dec 6, 2011, 9:32:10 AM12/6/11
to django...@googlegroups.com

Amazing….12 queries left and that’s hell of a lot faster J

 

Thank you guys !

Tom Evans

unread,
Dec 6, 2011, 9:38:15 AM12/6/11
to django...@googlegroups.com
On Tue, Dec 6, 2011 at 2:24 PM, Felipe Morales <felipe...@gmail.com> wrote:
> Hi,
>
> Maybe I can't understand you, but try with
>
> u = User.objects.filter(groups__name__in =
> gruppe).exclude(groups__name__in='Timesheet-Boss','TimeSheet-Manager','Projektleiter','Normal-User','Manager']).values_list('id',
> 'username')
>

That won't return the same results, it will exclude users who have a
group with one of the proscribed names, but are also in another group
that is not, which the original code did not do.

Cheers

Tom

Reply all
Reply to author
Forward
0 new messages