views:

1165

answers:

3

Hi all,

in my Django app i have a Newsletter model. Now I'd like to be able to send the newsletter (and even resend it) from Django Admin.

I could do this with a hook on the Model.save() method but is there another way that is not tied to the Model?

Thanks

+2  A: 

Admin actions (currently available in the development version, which is in beta) allow you to easily hook up custom actions which can be performed on selected items from the admin's list pages.

insin
This seems to be a good solution. I don't think it would be a big problem to update my site, but how did people do this before Django 1.1?
L. De Leo
This feature actually started out as a third-party app: http://code.google.com/p/django-batchadmin/
insin
A: 

If you are doing it from the admin then you'll need to override the save() method, but it can be the AdminModel save... doesn't need to be the full Model save.

However, if you are emailing a lot of emails, a better approach would be to install django-mailer which puts emails into a queue for later processing and then provides you with a new management command: send_mail.

So once you're ready to send the newsletter out you can manually run python manage.py send_mail. Any emails with errors will be moved to a deferred queue where you can retry sending them later.

You can automate this by running manage.py send_mail from cron.

If you really want to get fancy and do it from admin site, install django-chronograph and set up your send_mail schedule from there.

Van Gale
What would be the difference between a ModelAdmin.save() and a Model.save()?
L. De Leo
Meaning instead of saving in your Model in models.py you save in your admin.ModelAdmin class in admin.py.
Van Gale
See docs here: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#save-model-self-request-obj-form-change
Van Gale
A: 

I did this in Django-1.0.2 by using custom admin form with an additional boolean field, and checking for this field in save_model() method of my model admin class, see http://github.com/CrowdSense/django-subscription/blob/2a01389bb13244582df2c8b1e0fdd7039724eccf/subscription/admin.py#L28 lines 28-63. Actions for django-devel or django-batch-admin are also supplied in the file, but they are ignored by django-stable.

Maciej Pasternacki