Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#12696 closed (worksforme)

ModelAdmin.save_model is not called on creation, only on update

Reported by: David Seddon Owned by: nobody
Component: contrib.admin Version: 1.2-alpha
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Using Django version 1.2 alpha 1 and Python 2.6

I've extended an admin.ModelAdmin class and used the documented save_model method. This only appears to be called when an object is updated from the admin interface, but not when it is created. This is contrary to the documentation.

(HEALTH WARNING: I'm new to Django, so may have misconfigured this, but I don't think I have.)

Change History (5)

comment:1 by Karen Tracey, 15 years ago

Resolution: worksforme
Status: newclosed

I can't recreate with the provided information. Creating a ModelAdmin for a random model lying around:

class EventAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        if change:
            print 'save_model called for change'
        else:
            print 'save_model called for new object creation'
        obj.save()

from ttt.models import Event
admin.site.register(Event, EventAdmin)

using the development server I see prints for both new add and change:

Django version 1.2 alpha 1 SVN-12299, using settings 'playground.settings'
Development server is running at http://0.0.0.0:7777/
Quit the server with CONTROL-C.
[26/Jan/2010 12:18:43] "GET /admin/ HTTP/1.1" 200 7858
[26/Jan/2010 12:18:47] "GET /admin/ttt/event/add/ HTTP/1.1" 200 4895
[26/Jan/2010 12:18:47] "GET /admin/jsi18n/ HTTP/1.1" 200 1861
save_model called for new object creation
[26/Jan/2010 12:19:08] "POST /admin/ttt/event/add/ HTTP/1.1" 302 0
[26/Jan/2010 12:19:08] "GET /admin/ttt/event/ HTTP/1.1" 200 4770
[26/Jan/2010 12:19:22] "GET /admin/ttt/event/3/ HTTP/1.1" 200 5073
[26/Jan/2010 12:19:22] "GET /admin/jsi18n/ HTTP/1.1" 200 1861
save_model called for change
[26/Jan/2010 12:19:24] "POST /admin/ttt/event/3/ HTTP/1.1" 302 0
[26/Jan/2010 12:19:24] "GET /admin/ttt/event/ HTTP/1.1" 200 4772

comment:2 by David Seddon, 15 years ago

Strange, I get different results:

<pre>
class SnippetAdmin(admin.ModelAdmin):

list_display = ('name', 'description')

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

if change:

print 'save_model called for change'

else:

print 'save_model called for new object creation'

obj.save()


admin.site.register(Snippet, SnippetAdmin)
</pre>

Django version 1.2 alpha 1, using settings 'pepperpotdesign.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[28/Jan/2010 05:16:23] "GET /admin/snippets/snippet/add/ HTTP/1.1" 200 4202
[28/Jan/2010 05:16:23] "GET /admin/jsi18n/ HTTP/1.1" 200 1861
[28/Jan/2010 05:16:31] "POST /admin/snippets/snippet/add/ HTTP/1.1" 302 0
[28/Jan/2010 05:16:31] "GET /admin/snippets/snippet/ HTTP/1.1" 200 4023
[28/Jan/2010 05:16:35] "GET /admin/snippets/snippet/26/ HTTP/1.1" 200 4328
[28/Jan/2010 05:16:35] "GET /admin/jsi18n/ HTTP/1.1" 200 1861
save_model called for change
[28/Jan/2010 05:16:41] "POST /admin/snippets/snippet/26/ HTTP/1.1" 302 0
[28/Jan/2010 05:16:41] "GET /admin/snippets/snippet/ HTTP/1.1" 200 4017

comment:3 by David Seddon, 15 years ago

Oops - here's the code formatted properly:

class SnippetAdmin(admin.ModelAdmin):

    list_display = ('name', 'description')

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

        if change:

            print 'save_model called for change'

        else:

            print 'save_model called for new object creation'

        obj.save()

admin.site.register(Snippet, SnippetAdmin?)

Maybe because I'm not using the SVN version?

comment:4 by Karen Tracey, 15 years ago

Version: 1.11.2-alpha

Yes, it looks like this is a bug in the 1.2 alpha, related to the model validation changes, I suspect fixed by r12206. Most of the discussion around the issues there focused on problems where previously-valid code would now raise errors; I did not realize part of the problem was that save_model was no longer being called at all for new object creation. It has been fixed in current trunk.

comment:5 by David Seddon, 15 years ago

Cool thanks. In future I won't post bugs until testing with SVN version!

Note: See TracTickets for help on using tickets.
Back to Top