如何在 Django 中从 csv 文件自动创建表及其相关 model.py
想一想:
您创建了某种 CMS,它要求您提供应用程序名称和该应用程序的 csv 文件。 然后它会自动动态创建该应用程序,根据 csv 列创建所需的 model.py,激活它的管理页面,并且只允许您通过 django 对这个新表拥有完全权限admin,然后它将应用程序插入 url.py 并为其创建 view.py。
然后你所要做的就是上传一个 csv,命名你的应用程序,然后你就拥有了一个可以使用的管理页面。
现在,有没有办法在 django 中从 csv 文件创建一个应用程序或至少创建一个 model.py 或者是否有任何 django-app 可以做到这一点?
注意:超越 (./manage.pyspectrdb>models.py)
Think of this:
You create a CMS of some sort, which asks you for an application name and a csv file for that application.
Then it automatically creates that app on the fly, creates the required model.py based on the csv columns, activates the admin page for it and allows only you to have the full permission to this new table via django admin, then it inserts the the app into the url.py and creates the view.py for it as well.
Then all you'd have to do is upload a csv, name your app and whola!, you have an admin page to play with.
Now, is there anyway to create an app or at least a model.py out of a csv file in django or is there any django-app that can do this?
Note: Look beyond (./manage.py inspectdb > models.py)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然这不涉及创建实际的 models.py 和应用程序,但您可能需要研究动态创建模型类在运行时。您可以使用“元”模型来存储动态模型上的信息,然后让 CSV 视图将数据导入到这些模型中、创建类并向管理员注册。或者类似的东西。
使用 models.py、views.py 等创建实际的应用程序目录相当简单(只需创建目录、创建文件,然后根据 CSV 数据向其中写入格式化字符串)。编辑项目的settings.py和urls.py并重新加载模块也不会太困难。但是,如果不先查看自动生成的 Django 应用程序,我不会信任它们。
While this does not involve creating an actual models.py and application, you may want to look into dynamically creating Model classes at runtime. You could have "meta" models that store the information on the dynamic models, and then have your CSV view import the data into those models, create the classes, and register them with the admin. Or something like that.
Creating an actual application directory, with models.py, views.py, and so on, is fairly easy (just create the directory, create the files, and write formatted strings to them based on the CSV data). Editing the project's settings.py and urls.py, and reloading the modules, wouldn't be too difficult either. But, I wouldn't trust automatically generated Django applications without first looking at them.