以编程方式创建 django 模型
我一直在使用 django 为菜单系统编写一个相当复杂的面向对象模型。
最近我考虑了允许管理员创建“build_menu”对象的想法。
理想情况下,可以为输入提供一个名称,一旦创建,菜单将具有:
- 调用 django startapp 菜单
- 从菜单应用程序复制views.py、models.py 和 admin.py,用类名替换类名
- 附加到 urls.py 中包括“菜单”的新区域
- 将“.prefix_menu”附加到settings.py中的INSTALLED_APPS中
- python管理.py同步数据库
- pkill python(在Dreamhost上,所以我需要重新启动该进程看到屏幕上的新变化)
显然没有通用的实现可以为我做到这一点,但更大的问题是:这可能吗?是否可以将这些命令写入脚本以从服务器端执行此操作,并在 django 管理站点上创建新的“build_menu”对象后,让它运行该脚本,然后在完成时刷新页面?或者这是我无法从管理站点执行的操作?
I have been using django to write a rather complicated object-oriented model for a menu system.
Recently I have considered the idea of allowing the administrator to create a 'build_menu' object.
Ideally one would provide a name for input, and once created the menu would have:
- call django startapp menu
- copy of views.py, models.py, and admin.py from the menu app, replacing classnames with classnames
- append to urls.py to include the new areas for 'menu'
- append '.prefix_menu' to the INSTALLED_APPS in settings.py
- python manage.py syncdb
- pkill python (on Dreamhost so I'd need to restart the process to see new changes on the screen)
Obviously there is no generic implementation that will do this for me, but the bigger question is: Is this possible? Would it be possible to write these commands into a script to do it from the server side, and after creating a new 'build_menu' object on the django admin site, have it run that script and then refresh the page when it completes? Or is this something I would be unable to do from the admin site?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许我不明白你想要完成什么,但这对我来说似乎没有多大意义。如果没有修改,为什么要复制views.py等?你会有 N 个相同代码的副本,这是没有意义的。听起来您想要一个带有额外
name
列的模型,而不是 N 个模型。Maybe I don't understand what you're trying to accomplish, but it doesn't seem to make much sense to me. Why copy views.py, etc, if they aren't modified? You'd have N copies of the same code, there's no point. It sounds like you want one model with an extra
name
column, rather than N models.一种让它们在管理中单独显示的快速、hacky 方法是:
type_name
的单一模型,正如 Ned 建议的那样,type_name
添加到 <模型管理的 code>list_filter 属性type_name
的唯一值,并附上相应过滤器页面的链接这有一些相当明显的问题(例如,保存对象后,它会出现问题)将使您返回到未过滤的编辑页面),但您可能可以覆盖管理列表视图以及其他一些管理功能来很好地完成您想要的操作 - 无需更改代码。
One quick, hacky method to get them to show up separately in the admin would be:
type_name
, as Ned suggestedtype_name
to thelist_filter
attribute of the model admintype_name
with a link to the appropriate filter pageThat has some fairly obvious problems (e.g. after saving an object it'll bring you back to the unfiltered edit page), but you could probably override the admin list view and maybe some other admin functions to do what you want nicely -- no code altering required.