Django:如何动态创建模型 [英] Django: how do i create a model dynamically

查看:395
本文介绍了Django:如何动态创建模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在上传csv文件后动态创建模型?我已经完成了可以读取csv文件的部分。

How do I create a model dynamically upon uploading a csv file? I have done the part where it can read the csv file.

推荐答案

此文档很好地解释了如何在Django的运行时动态创建模型。

This doc explains very well how to dynamically create models at runtime in django. It also links to an example of doing so.

但是,正如您在查看文档后所看到的那样,这样做非常繁琐。我不建议您这样做,并且相信您很有可能可以提前确定足够灵活以处理CSV的模型。这将是更好的做法,因为在应用程序运行时动态更改数据库的架构是代码中大量错误的秘诀。

However, as you will see after looking at the document, it is quite complex and cumbersome to do this. I would not recommend doing this and believe it is quite likely you can determine a model ahead of time that is flexible enough to handle the CSV. This would be much better practice since dynamically changing the schema of your database as your application is running is a recipe for a ton of bugs in your code.

这篇关于Django:如何动态创建模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆