0

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 ...

7
  • "...runs whenever someone hits "save"...". Why aren't you using the model's save() method for this? Commented Apr 26, 2011 at 17:35
  • It can, but you may also run into permission problems since the webserver runs as another user. Have you tried just incorporating the necessary Python code into the handler? Commented Apr 26, 2011 at 17:43
  • @S.Lott - I don't see a save() method defined in the model, is it implicit? I'm not entirely sure what happens under the hood when a user hits the save button in the gui, frankly ... like I said, very new to Django. Commented Apr 26, 2011 at 17:52
  • 1
    @Beekguk: Please do the entire Django tutorial. It's in there. You'll see how it works. Commented Apr 26, 2011 at 17:55
  • @Keith - the script that needs to run is sort of an administrative tool that calls a bunch of other things - scripts that archive old files, ones that send various notifications, etc. So that particular code could get incorporated into the handler, but I want to avoid messing with the rest of it if I possibly can. I'm not sure how I'd do that, in any case. Commented Apr 26, 2011 at 17:58

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.