8

I want to export the results I have in a Queryset that I obtain from a haystack search view. In order to do this, I've found the best way is by doing it asyncronally, so I'm using Celery and Rabbitmq to manage the task and there create the file and iterate over all the results and then notify the user via email that the file is ready for them to grab it. However, in order to pass Celery the QuerySet, I need to serialize it.

Is there a quick way to do this? Or should I copy the request parameters and redo the search?

Julian
  • 702
  • 4
  • 14
  • 1
    It's not a best practice to pass model objects or QeurySets to tasks (see http://ask.github.com/celery/userguide/tasks.html#state). But it sounds like you may be in the clear here as long as you don't change any database state in the task afterwards. As to how to serialize it, that should usually work fine as long as you use Task.serializer="pickle" (default), there might be some objects in the evaluated queryset that is not pickleable. Maybe you could include the traceback pickle gives? – asksol Jun 24 '10 at 08:05

1 Answers1

5

You can serialize the Haystack QuerySet to JSON like this:

from django.core import serializers
serializers.serialize("json", [q.object for q in queryset])
Herman Schaaf
  • 46,821
  • 21
  • 100
  • 139