Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I "pickle" instances of Django models in a database into sample python code I can use to load sample data?

How do I "pickle" instances of Django models in a database into sample python code I can use to load sample data?

I want to:
1) Take a snapshot of several hundred records that I have stored in a MySQL database for a Django project
2) Take this snapshot and modify the data in it (blanking out names)
3) Transform this data into a "pickled string" or actual python code that I can use to load the data into a new users account.

The main feature I'm trying to implement is to select one of my current active Django website users , copy and anonymize some of their data, and then load that as sample data for all new users of the website so they can use that data to help learn the system.

like image 749
MikeN Avatar asked Jan 25 '12 22:01

MikeN


People also ask

How do you load a pickle dataset in Python?

Python Pickle load You have to use pickle. load() function to do that. The primary argument of pickle load function is the file object that you get by opening the file in read-binary (rb) mode. Simple!

How do you pickle a model in Python?

To save the ML model using Pickle all we need to do is pass the model object into the dump() function of Pickle. This will serialize the object and convert it into a “byte stream” that we can save as a file called model. pkl .

What is pickling in Python with example?

Pickle in Python is primarily used in serializing and deserializing a Python object structure. In other words, it's the process of converting a Python object into a byte stream to store it in a file/database, maintain program state across sessions, or transport data over the network.

How do you pickle and Unpickle a model in Python?

In a nutshell Using pickle , simply save your model on disc with dump() function and de-pickle it into your python code with load() function. Use open() function to create and/or read from a . pkl file and make sure you open the file in the binary format by wb for write and rb for read mode.


2 Answers

An easy way to do that would be to convert the model to a dict. Then, you can trivially pickle that and then re-inflate it to create new model instances.

To store the model as a dict, you can use a built-in Django function:

from django.forms.models import model_to_dict
my_dict = model_to_dict(my_instance,fields=[],exclude=[])

Once you've converted the instance to a dict and redacted what's necessary, just use the normal pickle.dumps and pickle.loads methods to store and retrieve the data. To create a new model instance using that dict, you can do something like:

my_instance = MyModel(**my_dict)
#add any customization for the new instance here
my_instance.save()
like image 182
Michael C. O'Connor Avatar answered Sep 26 '22 00:09

Michael C. O'Connor


On Django version 1.8 and above, if your models has foreign keys, you can use:

my_dict = dict([(f.attname, getattr(instance, f.attname))
               for f in instance._meta.get_fields()
               if hasattr(f, 'attname')])
like image 29
Magarato Avatar answered Sep 24 '22 00:09

Magarato