Django Selective Dumpdata

后端 未结 4 1404
醉话见心
醉话见心 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:11

    This isn't a simple answer to my question, but I found some interesting docs on Django's built-in natural keys feature, which would allow representing serialized records without the primary key. Unfortunately, it doesn't look like this is fully integrated into dumpdata, and there's an old outstanding ticket to fully rely on natural keys.

    It also seems the serializers.serialize() function allows serialization of an arbitrary list of specific model instances.

    Presumably, if I implemented a natural_key() method on all my models, and then called serializers.serialize([Users.objects.filter(criteria)]), it should come close to accomplishing what I want. I might have to write a function to crawl all the FK references, and include those in the list of objects passed to serialize().

提交回复
热议问题