Serialize QuerySet Q (not the result)

101 views
Skip to first unread message

guettli

unread,
Apr 20, 2015, 9:01:06 AM4/20/15
to django...@googlegroups.com
We want to store the QuerySet query (not the result) some how.

Background: users should be able to save a complex query as "my favorite query".

Pickling querysets is possible, but version updates are not supported:

  https://docs.djangoproject.com/en/1.8/ref/models/querysets/#pickling-querysets

Simple queries (without OR) could be saved as dictionary.

I have in mind some mini language which enables to store Q object instances:

 Q(foo='bar', blu='bla') | Q(foo='x', blu='y')

And ordering ....

Before reinventing the wheel, I want to ask if someone has seen or done
something like this before.

I could not find an existing project. But maybe I used the wrong keywords for
my favorite search engine.

Regards,
  Thomas Güttler

Vijay Khemlani

unread,
Apr 20, 2015, 11:12:21 AM4/20/15
to django...@googlegroups.com
Are the queries associated with a certain form? Or they are arbitrary queries?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3f75c38e-d2c9-480c-9103-e4ea1bad8f88%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

guettli

unread,
Apr 21, 2015, 7:00:32 AM4/21/15
to django...@googlegroups.com


Am Montag, 20. April 2015 17:12:21 UTC+2 schrieb Vijay Khemlani:
Are the queries associated with a certain form? Or they are arbitrary queries?


Good question. I would prefer a solution which is not associated with a form. It is
only associated with a model.

Otherwise it would be easy. I just need to store the input values of the form.

I know that is very hard to make all filtering features available. But at least the basic
filtering should be serializable.

Regards,
  Thomas Güttler
 

Vijay Khemlani

unread,
Apr 21, 2015, 8:35:52 AM4/21/15
to django...@googlegroups.com
Do you only need to serialize Q objects? No aggregation / annotations?

guettli

unread,
Apr 22, 2015, 5:18:11 AM4/22/15
to django...@googlegroups.com
Up to now only Q objects. If you know how to serialize aggregation / annotations, this would be great :-)

Jani Tiainen

unread,
Apr 22, 2015, 5:27:24 AM4/22/15
to django...@googlegroups.com
Hi,

Straight from the docs:

https://docs.djangoproject.com/en/1.8/ref/models/querysets/#pickling-querysets

"If you only want to pickle the necessary information to recreate the QuerySet from the database at a later time, pickle the query attribute of the QuerySet."

And in same place there even exists sample code how to restore pickled query.


On Wed, 22 Apr 2015 02:18:11 -0700 (PDT)
guettli <h...@tbz-pariv.de> wrote:

> Up to now only Q objects. If you know how to serialize aggregation /
> annotations, this would be great :-)
>
> Am Dienstag, 21. April 2015 14:35:52 UTC+2 schrieb Vijay Khemlani:
> >
> > Do you only need to serialize Q objects? No aggregation / annotations?
> >
> > On Tue, Apr 21, 2015 at 8:00 AM, guettli <h...@tbz-pariv.de <javascript:>>
> >>>> <https://groups.google.com/d/msgid/django-users/3f75c38e-d2c9-480c-9103-e4ea1bad8f88%40googlegroups.com?utm_medium=email&utm_source=footer>
> >>>> .
> >>>> For more options, visit https://groups.google.com/d/optout.
> >>>>
> >>>
> >>> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Django users" group.
> >> To unsubscribe from this group and stop receiving emails from it, send an
> >> email to django-users...@googlegroups.com <javascript:>.
> >> To post to this group, send email to django...@googlegroups.com
> >> <javascript:>.
> >> Visit this group at http://groups.google.com/group/django-users.
> >> To view this discussion on the web visit
> >> https://groups.google.com/d/msgid/django-users/a8cc7fc6-4969-4d88-b39f-bd1511cfed54%40googlegroups.com
> >> <https://groups.google.com/d/msgid/django-users/a8cc7fc6-4969-4d88-b39f-bd1511cfed54%40googlegroups.com?utm_medium=email&utm_source=footer>
> >> .
> >>
> >> For more options, visit https://groups.google.com/d/optout.
> >>
> >
> >
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a4541c8f-9056-4e09-8a6b-a55fa2367b26%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


--
Jani Tiainen

Tom Evans

unread,
Apr 22, 2015, 11:37:59 AM4/22/15
to django...@googlegroups.com
On Wed, Apr 22, 2015 at 10:27 AM, Jani Tiainen <red...@gmail.com> wrote:
> Hi,
>
> Straight from the docs:
>
> https://docs.djangoproject.com/en/1.8/ref/models/querysets/#pickling-querysets
>
> "If you only want to pickle the necessary information to recreate the QuerySet from the database at a later time, pickle the query attribute of the QuerySet."
>
> And in same place there even exists sample code how to restore pickled query.
>

And one paragraph later it says:

"""
You can’t share pickles between versions

Pickles of QuerySets are only valid for the version of Django that was
used to generate them. If you generate a pickle using Django version
N, there is no guarantee that pickle will be readable with Django
version N+1.
"""

> Background: users should be able to save a complex query as "my favorite query".
>
> Pickling querysets is possible, but version updates are not supported:

I think the OP is aware of this, and is looking for an alternate solution.

Cheers

Tom
Reply all
Reply to author
Forward
0 new messages