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

后端 未结 2 421
轻奢々
轻奢々 2021-02-06 09:43

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

2条回答
  •  渐次进展
    2021-02-06 10:36

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

提交回复
热议问题