Friday, September 2, 2011

Re: Pickling a QuerySet

Thanks for this.

I have a question. Is it possible to pickle a QuerySet and write to csv?

I have been trying to use pickle to achieve the same stuff you just did. But
I guess I've not been patient enough.

Thanks

On Fri, Sep 2, 2011 at 11:17 AM, Kevin <kveroneau@gmail.com> wrote:
Wow!  This is very interesting and I did not expect the results.

The following:
>>> dat = pickle.loads(open('kbase.dat','rb').read())
>>> for item in dat:
...   item.save()

Worked flawlessly!  It recreated all the data in the database that I
removed to see what would happen if I unpickled it and saved it all.

I just attempted to also do the same with the data already existing,
and it did not create new entries in the database, but merely update
the existing ones.

Are there ways to test if the entry exists in the database after
unpickling the data?  As it currently stands it will overwrite
existing data with the new values contained in the pickle.

This would definitely be an interesting take on data backup or even
migration to a new database server.

On Sep 2, 5:05 am, Kevin <kveron...@gmail.com> wrote:
> An interesting question here, not sure if anybody has tried to pickle
> their QuerySets before.
>
> Here's an example that does work:
>         entry_list = Entry.objects.all()
>         response =
> HttpResponse(pickle.dumps(entry_list),mimetype='application/x-
> pickle.python')
>         response['Content-Disposition'] = 'attachment; filename=kbase.dat'
>         return response
>
> This is one of my views.  Now, I have another view that takes a file
> upload to take in this pickled data.  My objective is to be-able to
> export an entire model, or select items in a model.
>
> It seems to unpickle just fine in the same site, and all the entry
> objects in the queryset seem to be intact.  Now, what if, the target
> site did not have the same entry objects and this pickle was imported
> in?  The model is the exact same, just the actual data in the database
> is different.  Is there an easy way to compare for differences and
> sync the pickled data with the data in the database?  I won't run into
> any conflicts as data is never removed on either end, data is just
> merely added on one site, and I want to manually sync it with a
> different site.
>
> I'll be running a few tests to see what happens with this data and
> post back here to let anybody who wants to know, know.

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




--
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde

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

No comments:

Post a Comment