简体   繁体   English

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

[英]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? 如何在数据库中“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 1)拍摄我存储在MySQL数据库中的几百条记录的快照,用于Django项目
2) Take this snapshot and modify the data in it (blanking out names) 2)拍摄此快照并修改其中的数据(消隐名称)
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. 3)将此数据转换为“pickled string”或实际的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网站用户之一,复制和匿名他们的一些数据,然后将其作为网站所有新用户的样本数据加载,以便他们可以使用该数据帮助学习系统。

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: 要将模型存储为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. 一旦将实例转换为dict并编辑了必要的内容,只需使用普通的pickle.dumpspickle.loads方法来存储和检索数据。 To create a new model instance using that dict, you can do something like: 要使用该dict创建新的模型实例,您可以执行以下操作:

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

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')])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在代码中正确使用random.sample? - How can i properly use random.sample in my code? 如何在 Django 中为模型分配实例? - How do I assign instances to models in django? 如何在此代码段中使用pickle? - How do I use pickle in this code snippet? 如何在Python的单行代码中使用for循环打印随机样本(带有random.sample(range(x,y),z))? - How can I print a random sample (with random.sample(range(x,y), z)) using a for-loop in a SINGLE line of code in Python? 如何从 Python 的数据集中获取第二个样本,而不从第一个样本中获取重复? - How I do I get a second sample from a dataset in Python without getting duplication from a first sample? 我无法在Google图表的代码JavaScript中获取JSON数据(项目Python Hello仪表板示例) - I can't get json data in code javascript for google chart (project Python Hello Dashboard sample) 如何从python脚本中采样数据库表中一定数量的行? - How do I sample a certain number of rows in a database table from a python script? 我在哪里可以下载Django示例模板? - Where can I download a sample Django template? 如何将下面的示例代码转换为接受多个输入? - How do I turn the sample code below into accepting multiple inputs? 如何运行 tensorflow 示例代码? (抛出错误) - How do I run the tensorflow sample code? (throws an error)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM