Passing a dictionary as a query argument?

32 views
Skip to first unread message

oli...@obeattie.com

unread,
Dec 13, 2007, 3:08:16 PM12/13/07
to Django users
Hey All,

I've just written a custom model field for storing anything which is
pickle-able (generally this will be used for storing dictionaries). It
all works great, apart from when I want to perform a query on objects
using the field as an argument. When doing something like:

SomeModel.objects.filter(my_field={'foo': 'bar', 'bar': 'baz'})

However, it seems to reduce my dictionary to an iterable containing
just the dictionary keys (so, in this case ['foo', 'bar']). I presume
Django is doing this by iterating over the dictionary, but obviously
this doesn't work for my field. Is there any way to stop Django from
doing this, away from overriding query methods in the manager to
convert the dictionary to something non-iterable?

Any help much appreciated!

Thanks,
O

oli...@obeattie.com

unread,
Dec 16, 2007, 7:52:30 AM12/16/07
to Django users
I fixed this -- not Django's fault at all. For the full field code, go
to http://www.djangosnippets.org/snippets/513/

Ned Batchelder

unread,
Dec 17, 2007, 7:08:16 AM12/17/07
to django...@googlegroups.com
If I understand what you're doing here, you need to be very careful.  Converting a dictionary to a string (or pickle) does not guarantee that two equal dictionaries convert into two equal strings (or pickles), especially if your version of Python changes.  For example, your code below may create a pickle with the key 'foo' first, or it may create a pickle with the key 'bar' first.  Both pickles un-pickle to an equivalent dictionary, but the pickles won't compare as equal, and your queries will fail.

I could be wrong, and the pickle module provides some guarantee about the equivalency of pickles, but I don't think so.

--Ned.
http://nedbatchelder.com/blog
-- 
Ned Batchelder, http://nedbatchelder.com

ivan.illarionov

unread,
Dec 20, 2007, 12:26:10 AM12/20/07
to Django users
It would be safer to also use
base64.encodestring(pickle.dumps(python_data)) and
pickle.loads(base64.decodestring(db_data))
just like Django's db cache does. Pickle data is binary and is not
good for TextField

On 17 дек, 15:08, Ned Batchelder <n...@nedbatchelder.com> wrote:
> If I understand what you're doing here, you need to be very careful.
> Converting a dictionary to a string (or pickle) does not guarantee that
> two equal dictionaries convert into two equal strings (or pickles),
> especially if your version of Python changes. For example, your code
> below may create a pickle with the key 'foo' first, or it may create a
> pickle with the key 'bar' first. Both pickles un-pickle to an
> equivalent dictionary, but the pickles won't compare as equal, and your
> queries will fail.
>
> I could be wrong, and the pickle module provides some guarantee about
> the equivalency of pickles, but I don't think so.
>
> --Ned.http://nedbatchelder.com/blog
>
>
>
> oli...@obeattie.com wrote:
> > I fixed this -- not Django's fault at all. For the full field code, go
> > tohttp://www.djangosnippets.org/snippets/513/

Oliver Beattie

unread,
Dec 27, 2007, 3:54:00 AM12/27/07
to Django users
I think the pickle modules guarantee that both pickle and cPickle will
be able to read each other's pickles, but that's about it. Aside from
unpickling every database object on every database query, though, I
see no other alternative (aisde from writing my own pickle module
[eek]).

And thanks for the base64 tip, will do this.

On Dec 17, 12:08 pm, Ned Batchelder <n...@nedbatchelder.com> wrote:
> If I understand what you're doing here, you need to be very careful.  
> Converting a dictionary to a string (or pickle) does not guarantee that
> two equal dictionaries convert into two equal strings (or pickles),
> especially if your version of Python changes.  For example, your code
> below may create a pickle with the key 'foo' first, or it may create a
> pickle with the key 'bar' first.  Both pickles un-pickle to an
> equivalent dictionary, but the pickles won't compare as equal, and your
> queries will fail.
>
> I could be wrong, and the pickle module provides some guarantee about
> the equivalency of pickles, but I don't think so.
>
> --Ned.http://nedbatchelder.com/blog
>
>
>
> oli...@obeattie.com wrote:
> > I fixed this -- not Django's fault at all. For the full field code, go
> > tohttp://www.djangosnippets.org/snippets/513/
Reply all
Reply to author
Forward
0 new messages