如何在 Django 中从 csv 文件自动创建表及其相关 model.py

发布于 2024-09-11 12:35:40 字数 397 浏览 1 评论 0原文

想一想:

您创建了某种 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝色星空 2024-09-18 12:35:40

虽然这不涉及创建实际的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文