Django Selective Dumpdata

后端 未结 4 1480
醉话见心
醉话见心 2021-02-01 13:31

Is it possible to selectively filter which records Django\'s dumpdata management command outputs? I have a few models, each with millions of rows, and I only want to dump record

4条回答
  •  别跟我提以往
    2021-02-01 14:18

    This is a very old question, but I recently wrote a custom management command to do just that. It looks very similar to the existing dumpdata command except that it takes some extra arguments to define how I want to filter the querysets and it overrides the get_objects function to perform the actual filtering:

    def get_objects(dump_attributes, dump_values):
      qs_1 = ModelClass1.objects.filter(**options["filter_options_for_model_class_1"])    
      qs_2 = ModelClass2.objects.filter(**options["filter_options_for_model_class_2"])    
      # ...repeat for as many different model classes you want to dump...
      yield from chain(qs_1, qs_2, ...)
    
    

提交回复
热议问题