views:

244

answers:

3

One of the functionalities in a Django project I am writing is sending a newsletter. I have a model, Newsletter and a function, send_newsletter, which I have registered to listen to Newsletter's post_save signal. When the newsletter object is saved via the admin interface, send_newsletter checks if created is True, and if yes it actually sends the mail.

However, it doesn't make much sense to edit a newsletter that has already been sent, for the obvious reasons. Is there a way of making the Newsletter object read-only once it has been saved?

Edit:

I know I can override the save method of the object to raise an error or do nothin if the object existed. However, I don't see the point of doing that. As for the former, I don't know where to catch that error and how to communicate the user the fact that the object wasn't saved. As for the latter, giving the user false feedback (the admin interface saying that the save succeded) doesn't seem like a Good Thing.

What I really want is allow the user to use the Admin interface to write the newsletter and send it, and then browse the newsletters that have already been sent. I would like the admin interface to show the data for sent newsletters in an non-editable input box, without the "Save" button. Alternatively I would like the "Save" button to be inactive.

+4  A: 

You can check if it is creation or update in the model's save method:

def save(self, *args, **kwargs):
    if self.pk:
        raise StandardError('Can\'t modify bla bla bla.')
    super(Payment, self).save(*args, **kwargs)

Code above will raise an exception if you try to save an existing object. Objects not previously persisted don't have their primary keys set.

muhuk
Heh - I was in the middle of writing this same answer when it popped up...
Carl Meyer
+1  A: 

Suggested reading: The Zen of Admin in chapter 17 of the Django Book.

Summary: The admin is not designed for what you're trying to do :(

However, the 1.0 version of the book covers only Django 0.96, and good things have happened since.

In Django 1.0, the admin site is more customizable. Since I haven't customized admin myself, I'll have to guess based on the docs, but I'd say overriding the model form is your best bet.

akaihola
A: 

use readonlyadmin in ur amdin.py.List all the fields which u want to make readonly.After creating the object u canot edit them then

use the link

http://www.djangosnippets.org/snippets/937/

copy the file and then import in ur admin.py and used it

ha22109