How to create an instance of read-only model after saving it once?

One of the features of the Django project that I am writing is sending out a newsletter. I have a model Newsletterand a function send_newsletterthat I registered to listen to the signal Newsletter post_save. When a newsletter object is saved through the administrator’s interface, it send_newsletterchecks to see if createdTrue is set, and if so, it does send mail.

However, it makes no sense to edit a newsletter that has already been sent, for obvious reasons. Is there a way to make an object Newsletterread-only after saving it?

Edit:

I know that I can override the method of an saveobject to cause an error or to do nothing if the object existed. However, I see no reason to do this. As for the first, I don’t know where to catch this error and how to inform the user that the object was not saved. Regarding the latter, giving the user false feedback (admin interface saying that the saved save) is not like Good Thing.

I really want the user to be able to use the admin interface to write a newsletter and send it, and then view the newsletters that have already been sent. I would like the admin interface to display data for sent newsletters in an uneditable input field without the Save button. Alternatively, I would like the Save button to be inactive.

+3
source share
4 answers

You can check if this is a creation or update in the model method save:

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

, . , , .

+7

: Zen of Admin 17 Django.

: , : (

1.0 Django 0.96, .

Django 1.0 . , , , , .

+1

readonlyadmin ur amdin.py.List , readonly. u canot ,

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

, ur admin.py

0

, , :

class MyModelAdmin(ModelAdmin):
    form = ...
    def get_readonly_fields(self, request, obj=None):
        if obj:
            return MyModelAdmin.form.Meta.fields
        else: # This is an addition
            return []

Save, ,

  • has_change_permission return False
  • , (admin_modify.submit_row templatetag), show_save=True .

, :

  • has_change_permission :

    class NoSaveModelAdminMixin(object):
        def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
            response = super(NoSaveModelAdmin, self).render_change_form(request, context, add, change,form_url, obj)
            response.context_data["has_change_permission"] = self.has_real_change_permission(request, obj)
        def has_real_change_permission(self, request, obj):
            return obj==None
        def change_view(self, request, object_id, extra_context=None):
            obj = self.get_object(request, unquote(object_id))
            if not self.has_real_change_permission(request, obj) and request.method == 'POST':
                raise PermissionDenied 
            return super(NoSaveModelAdmin, self).change_view(request, object_id, extra_context=extra_context)
    
  • templatetag submit_row, :

    @admin_modify.register.inclusion_tag('admin/submit_line.html', takes_context=True)
    def submit_row(context):
        ...
            'show_save': context['has_change_permission']
        ...
    
    admin_modify.submit_row = submit_row
    
0

All Articles