QuerySets in Dictionaries

Discussion in 'Python' started by scoopseven, Nov 12, 2009.

  1. scoopseven

    scoopseven Guest

    I need to create a dictionary of querysets. I have code that looks
    like:

    query1 = Myobject.objects.filter(status=1)
    query2 = Myobject.objects.filter(status=2)
    query3 = Myobject.objects.filter(status=3)

    d={}
    d['a'] = query1
    d['b'] = query2
    d['c'] = query3

    Is there a way to do this that I'm missing?

    Thanks.
    scoopseven, Nov 12, 2009
    #1
    1. Advertising

  2. 2009/11/12 scoopseven <>:
    > I need to create a dictionary of querysets.  I have code that looks
    > like:
    >
    > query1 = Myobject.objects.filter(status=1)
    > query2 = Myobject.objects.filter(status=2)
    > query3 = Myobject.objects.filter(status=3)
    >
    > d={}
    > d['a'] = query1
    > d['b'] = query2
    > d['c'] = query3
    >
    > Is there a way to do this that I'm missing?


    Untested:

    wanted = (('a', 1), ('b', 2), ('c', 2))
    d = dict((key, Myobject.objects.filter(status=number)) for (key,
    number) in wanted)


    --
    Cheers,
    Simon B.
    Simon Brunning, Nov 12, 2009
    #2
    1. Advertising

  3. scoopseven schrieb:
    > I need to create a dictionary of querysets. I have code that looks
    > like:
    >
    > query1 = Myobject.objects.filter(status=1)
    > query2 = Myobject.objects.filter(status=2)
    > query3 = Myobject.objects.filter(status=3)
    >
    > d={}
    > d['a'] = query1
    > d['b'] = query2
    > d['c'] = query3
    >
    > Is there a way to do this that I'm missing?



    d = dict(a=Myobject.objects.filter(status=1),
    b=Myobject.objects.filter(status=2),
    c=Myobject.objects.filter(status=3))

    Diez
    Diez B. Roggisch, Nov 12, 2009
    #3
  4. On Thu, 12 Nov 2009 10:39:33 -0800, scoopseven wrote:

    > I need to create a dictionary of querysets. I have code that looks
    > like:
    >
    > query1 = Myobject.objects.filter(status=1)
    > query2 = Myobject.objects.filter(status=2)
    > query3 = Myobject.objects.filter(status=3)
    >
    > d={}
    > d['a'] = query1
    > d['b'] = query2
    > d['c'] = query3
    >
    > Is there a way to do this that I'm missing?


    I don't understand your problem. Assuming Myobject is defined, and has
    the appropriate attribute objects.filter, the above should work exactly
    as you give it.

    What is your actual problem? Are you asking for help in defining a
    queryset?



    --
    Steven
    Steven D'Aprano, Nov 13, 2009
    #4
  5. scoopseven

    scoopseven Guest

    On Nov 12, 8:55 pm, Steven D'Aprano <st...@REMOVE-THIS-
    cybersource.com.au> wrote:
    > On Thu, 12 Nov 2009 10:39:33 -0800, scoopseven wrote:
    > > I need to create a dictionary of querysets.  I have code that looks
    > > like:

    >
    > > query1 = Myobject.objects.filter(status=1)
    > > query2 = Myobject.objects.filter(status=2)
    > > query3 = Myobject.objects.filter(status=3)

    >
    > > d={}
    > > d['a'] = query1
    > > d['b'] = query2
    > > d['c'] = query3

    >
    > > Is there a way to do this that I'm missing?

    >
    > I don't understand your problem. Assuming Myobject is defined, and has
    > the appropriate attribute objects.filter, the above should work exactly
    > as you give it.
    >
    > What is your actual problem? Are you asking for help in defining a
    > queryset?
    >
    > --
    > Steven


    I actually had a queryset that was dynamically generated, so I ended
    up having to use the eval function, like this...

    d = {}
    for thing in things:
    query_name = 'thing_' + str(thing.id)
    query_string = 'Thing.objects.filter(type=' + str(thing.id) +
    ').order_by(\'-date\')[:3]'
    executable_string = query_name + ' = Thing.objects.filter
    (type=' + str(thing.id) + ').order_by(\'-date\')[:3]'
    exec(executable_string)
    d[query_name] = eval(query_string)

    And no, this is not based on user-generated input.

    Thanks for the input.
    scoopseven, Nov 13, 2009
    #5
  6. scoopseven schrieb:
    > On Nov 12, 8:55 pm, Steven D'Aprano <st...@REMOVE-THIS-
    > cybersource.com.au> wrote:
    >> On Thu, 12 Nov 2009 10:39:33 -0800, scoopseven wrote:
    >>> I need to create a dictionary of querysets. I have code that looks
    >>> like:
    >>> query1 = Myobject.objects.filter(status=1)
    >>> query2 = Myobject.objects.filter(status=2)
    >>> query3 = Myobject.objects.filter(status=3)
    >>> d={}
    >>> d['a'] = query1
    >>> d['b'] = query2
    >>> d['c'] = query3
    >>> Is there a way to do this that I'm missing?

    >> I don't understand your problem. Assuming Myobject is defined, and has
    >> the appropriate attribute objects.filter, the above should work exactly
    >> as you give it.
    >>
    >> What is your actual problem? Are you asking for help in defining a
    >> queryset?
    >>
    >> --
    >> Steven

    >
    > I actually had a queryset that was dynamically generated, so I ended
    > up having to use the eval function, like this...
    >
    > d = {}
    > for thing in things:
    > query_name = 'thing_' + str(thing.id)
    > query_string = 'Thing.objects.filter(type=' + str(thing.id) +
    > ').order_by(\'-date\')[:3]'
    > executable_string = query_name + ' = Thing.objects.filter
    > (type=' + str(thing.id) + ').order_by(\'-date\')[:3]'
    > exec(executable_string)
    > d[query_name] = eval(query_string)
    >
    > And no, this is not based on user-generated input.


    Why do you need this to use exec? And it seems that executable_string is
    superflous - or do you want the side-effect? But then, you have the
    results in d already.


    query = Thing.objects.filter(type="%i" % thing.id).order_by('-date')[:3]

    d[query_name] = query

    As a rule of thumb: if you think you need exec, you don't.


    Diez
    Diez B. Roggisch, Nov 14, 2009
    #6
  7. On Fri, 13 Nov 2009 14:10:10 -0800, scoopseven wrote:

    > I actually had a queryset that was dynamically generated, so I ended up
    > having to use the eval function, like this...
    >
    > d = {}
    > for thing in things:
    > query_name = 'thing_' + str(thing.id)
    > query_string = 'Thing.objects.filter(type=' + str(thing.id) +
    > ').order_by(\'-date\')[:3]'
    > executable_string = query_name + ' = Thing.objects.filter
    > (type=' + str(thing.id) + ').order_by(\'-date\')[:3]'
    > exec(executable_string)
    > d[query_name] = eval(query_string)



    What an unmaintainable mess.

    If I've understood it, you can make it less crap by (1) getting rid of
    the unnecessary escaped quotes, (2) avoiding generating the same strings
    multiple times, and (3) avoiding string concatenation.

    d = {}
    for thing in things:
    expr = "Thing.objects.filter(type=%s).order_by('-date')[:3]"
    expr = rhs % thing.id
    name = "thing_%s" % thing.id
    exec("%s = %s" % (name, expr))
    d[name] = eval(expr)


    What else can we do to fix it? Firstly, why are you creating local
    variables "thing_XXX" (where XXX is the thing ID) *and* dictionary keys
    of exactly the same name? I'm sure you don't need the local variables.
    (If you think you do, you almost certainly don't.) That gets rid of the
    exec.

    Next, let's get rid of the eval:

    d = {}
    for thing in things:
    x = thing.id
    name = "thing_%s" % x
    d[name] = Thing.objects.filter(type=x).order_by('-date')[:3]


    About half the size, ten times the speed, and 1000 times the readability.

    Unless I've missed something, you don't need either exec or eval.



    --
    Steven
    Steven D'Aprano, Nov 15, 2009
    #7
  8. scoopseven

    scoopseven Guest

    On Nov 14, 11:55 pm, Steven D'Aprano <st...@REMOVE-THIS-
    cybersource.com.au> wrote:
    > On Fri, 13 Nov 2009 14:10:10 -0800, scoopseven wrote:
    > > I actually had a queryset that was dynamically generated, so I ended up
    > > having to use the eval function, like this...

    >
    > > d = {}
    > > for thing in things:
    > >         query_name = 'thing_' + str(thing.id)
    > >         query_string = 'Thing.objects.filter(type=' + str(thing.id) +  
    > > ').order_by(\'-date\')[:3]'
    > >         executable_string = query_name + ' = Thing.objects.filter
    > > (type=' + str(thing.id) + ').order_by(\'-date\')[:3]'
    > >         exec(executable_string)
    > >         d[query_name] = eval(query_string)

    >
    > What an unmaintainable mess.
    >
    > If I've understood it, you can make it less crap by (1) getting rid of
    > the unnecessary escaped quotes, (2) avoiding generating the same strings
    > multiple times, and (3) avoiding string concatenation.
    >
    > d = {}
    > for thing in things:
    >     expr = "Thing.objects.filter(type=%s).order_by('-date')[:3]"
    >     expr = rhs % thing.id
    >     name = "thing_%s" % thing.id
    >     exec("%s = %s" % (name, expr))
    >     d[name] = eval(expr)
    >
    > What else can we do to fix it? Firstly, why are you creating local
    > variables "thing_XXX" (where XXX is the thing ID) *and* dictionary keys
    > of exactly the same name? I'm sure you don't need the local variables.
    > (If you think you do, you almost certainly don't.) That gets rid of the
    > exec.
    >
    > Next, let's get rid of the eval:
    >
    > d = {}
    > for thing in things:
    >     x = thing.id
    >     name = "thing_%s" % x
    >     d[name] = Thing.objects.filter(type=x).order_by('-date')[:3]
    >
    > About half the size, ten times the speed, and 1000 times the readability.
    >
    > Unless I've missed something, you don't need either exec or eval.
    >
    > --
    > Steven


    Steven,

    This worked like a charm! Thank you for your insight, it's greatly
    appreciated.

    Mark
    scoopseven, Nov 17, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TJS
    Replies:
    0
    Views:
    396
  2. Replies:
    1
    Views:
    1,533
    Roedy Green
    Jan 9, 2006
  3. Brainwashed

    Keys order in dictionaries

    Brainwashed, Jun 27, 2003, in forum: Python
    Replies:
    2
    Views:
    756
    Duncan Booth
    Jun 27, 2003
  4. lysdexia
    Replies:
    6
    Views:
    477
    John Machin
    Dec 2, 2007
  5. Brandon
    Replies:
    12
    Views:
    473
    Brandon
    Aug 15, 2008
Loading...

Share This Page