如何在数据库中“pickle”D​​jango模型的实例到我可以用来加载样本数据的示例python代码?

时间:2022-04-12 06:36:28

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

如何在数据库中“pickle”D​​jango模型的实例到我可以用来加载样本数据的示例python代码?

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.

我想:1)拍摄我存储在MySQL数据库中的几百条记录的快照,用于Django项目2)拍摄此快照并修改其中的数据(消隐名称)3)将此数据转换为“腌制”字符串“或我可用于将数据加载到新用户帐户的实际python代码。

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.

我想要实现的主要功能是选择我当前活跃的Django网站用户之一,复制并匿名他们的一些数据,然后将其作为网站所有新用户的样本数据加载,以便他们可以使用该数据帮助学习系统。

2 个解决方案

#1


12  

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.

一种简单的方法是将模型转换为dict。然后,你可以轻而易举地腌制它,然后重新膨胀它以创建新的模型实例。

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

要将模型存储为dict,可以使用内置的Django函数:

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:

一旦将实例转换为dict并编辑了必要的内容,只需使用普通的pickle.dumps和pickle.loads方法来存储和检索数据。要使用该dict创建新的模型实例,您可以执行以下操作:

my_instance = MyModel(**my_dict)
#add any customization for the new instance here
my_instance.save()

#2


1  

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

在Django 1.8及以上版本中,如果你的模型有外键,你可以使用:

my_dict = dict([(f.attname, getattr(instance, f.attname))
               for f in instance._meta.get_fields()
               if hasattr(f, 'attname')])

#1


12  

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.

一种简单的方法是将模型转换为dict。然后,你可以轻而易举地腌制它,然后重新膨胀它以创建新的模型实例。

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

要将模型存储为dict,可以使用内置的Django函数:

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:

一旦将实例转换为dict并编辑了必要的内容,只需使用普通的pickle.dumps和pickle.loads方法来存储和检索数据。要使用该dict创建新的模型实例,您可以执行以下操作:

my_instance = MyModel(**my_dict)
#add any customization for the new instance here
my_instance.save()

#2


1  

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

在Django 1.8及以上版本中,如果你的模型有外键,你可以使用:

my_dict = dict([(f.attname, getattr(instance, f.attname))
               for f in instance._meta.get_fields()
               if hasattr(f, 'attname')])