从 Django 管理界面调用外部脚本?

发布于 2024-11-03 14:25:22 字数 747 浏览 4 评论 0原文

我有一个 Django 管理界面,几乎仅用作 GUI 表单,用于对单个 postgresql 表进行更改。目前还有一个 Python 脚本,每当对数据库进行更改时,都会从命令行手动运行,&我想将其连接起来,以便每当有人通过管理界面对表的一行进行更改后点击“保存”时它就会运行。如果这是views.py中的条目,看起来我会将脚本作为模块导入并从视图运行其主要函数(即Django 可以使用链接到其他库的“外部”python 脚本(NumPy、 RPy2...))。但是,我不确定如何在管理界面中执行此操作。

  • admin.py 与views.py 中的常规条目有何相似/不同?
  • 我应该在哪里放置对外部脚本的导入/调用 - 模型中的某个位置,admin.py 中的某个位置?

我熟悉 Python,但对“网络内容”(即 Django 这样的框架)相当陌生(并且有些困惑),&我什至不确定我是否非常清楚地提出这个问题,因为我对视图/模型概念仍然有点模糊......

编辑:事实证明,事实上,通过阅读文档/教程找到了解决方案,但假设管理内容存在差异。正如基思在评论中提到的,我现在遇到了权限问题,但我想这是一个单独的问题。所以谢谢,&也许我会停止怀疑自己......

I have a Django admin interface that is used almost solely as a gui form for making changes to a single postgresql table. There's also a Python script that's currently run manually from the command line whenever a change is made to the database, & I'd like to hook that up so it runs whenever someone hits "save" after making a change to a row of the table via the admin interface. If this was an entry in views.py, it looks like I'd import the script as a module and run its main function from the view (ie, Can Django use "external" python scripts linked to other libraries (NumPy, RPy2...)). I'm not sure, however, how to do this in the admin interface.

  • How is admin.py similar/different to a regular entry in views.py?
  • Where do I put the import/call to the external script - somewhere in the model, somewhere in admin.py?

I'm familiar with Python, but am fairly new to (& somewhat mystified by) "web stuff" (ie, frameworks like Django), & I'm not even sure if I'm asking this question very clearly, because I'm still a little fuzzy on the view/model concept ...

Edit: Turns out I had, in fact, found the solution by reading the documentation/tutorial, but assumed there was a difference with admin stuff. As Keith mentioned in the comments, I'm now running into permissions issues, but I guess that's a separate problem. So thanks, & maybe I'll stop second guessing myself ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

内心激荡 2024-11-10 14:25:22

一般来说,您希望在“保存”时发生的事情是

  1. 模型的一部分。

    如果是这样,您可以重写模型的 save 方法:http://docs.djangoproject.com/en/1.3/ref/models/instances/# saving-objects

    您可以在该 save 方法中执行任何操作

  2. 部分视图功能。

    如果是这样,您要么扩展管理界面(不太容易),要么编写自己的界面。

Generally, things you want to happen at 'save' time are either

  1. Part of the model.

    If so, you override the model's save method: http://docs.djangoproject.com/en/1.3/ref/models/instances/#saving-objects

    You can do anything in that save method.

  2. Part of the view function.

    If so, you either extend the admin interface (not so easy), or you write your own.

爱人如己 2024-11-10 14:25:22

您可能需要考虑的一件事是在 ModelAdmin 中定义 save_model 方法。当有人从管理员保存时,这将被执行(但当有人在管理员之外进行保存时,则不会执行)。这种方法可能取决于您的要求,但在从管理员进行保存时应该为您提供必要的挂钩。

在admin.py中

class MyModelAdmin(admin.ModelAdmin):

    model = models.MyModel

    def save_model(self, request, obj, form, change):

        # you can put custom code in here
        obj.save()

One thing you might consider is defining the save_model method in your ModelAdmin. This will get executed when someone saves from the admin (but not when someone does a save outside of the admin). This approach might depend on what your requirements are, but should give you the necessary hook when doing the save from the admin.

In admin.py

class MyModelAdmin(admin.ModelAdmin):

    model = models.MyModel

    def save_model(self, request, obj, form, change):

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