Posts Tagged ‘add_view’

Modifying Django NewForms-Admin views

Friday, September 5th, 2008

Playing with the new Django features, specially with the NewForms-Admin, I was looking for a way to change the add view behavior for some models.

It’s really easy and obvious (it’s always easy and obvious with Python and Django).

First, you’ll need the admin.py file in your app directory.


from django.contrib import admin
from myproject.myapp.models import MyModel

class MyModelAdmin(admin.ModelAdmin):
def add_view(self, request):
if request.method == 'POST':
# do whatever you want
# remember, POSTing means that someone entered data.
return admin.ModelAdmin.add_view(self, request)

admin.site.register(MyModel, MyModelAdmin)

The add_view method is called when you try to add an entry using the admin. There are other interesting methods you should look too, for customizing the admin behavior (change_view, delete_view, etc).

Another way to customize things is to write your own ModelForm, but it’s beyond the scope of this post :)