admin管理员组文章数量:1310019
I've written a function to save a QuerySet to a fixture JSON file:
def save_as_fixture(query_set: QuerySet, fixture_name: str, app_label: str='mainapp'):
app_config = apps.get_app_config(app_label)
fixture_dir = os.path.join(app_config.path, "fixtures")
os.makedirs(fixture_dir, exist_ok=True)
fixture_path = os.path.join(fixture_dir, fixture_name)
data = serializers.serialize("json", query_set, indent=2, use_natural_foreign_keys=True, use_natural_primary_keys=True)
with open(fixture_path, 'w') as file:
file.write(data)
But it doesn't save related objects.
I would like it to also save the objects that reference one of the objects of the QuerySet via a ForeignKey
, OneToOneField
, etc. How can I do that?
本文标签: pythonSave all objects in QuerySet and related objects to a fixtureStack Overflow
版权声明:本文标题:python - Save all objects in QuerySet and related objects to a fixture - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741856872a2401393.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论