views:

84

answers:

1

on save if user select certain option i want to take user to a page where he can have one more field to be filled and then redirect to admin default page

A: 

Rather than redirect to a new page you can simply override the form that the create and update pages use. This lets you add additional fields to the modelform, keeping all your form processing in one place.

First you'll need to set up your ModelAdmin to use the custom form:

admin.py

from django.contrib import admin
from forms import BookAdminForm
from models import Book

class BookAdmin(admin.ModelAdmin):
    form = BookAdminForm

admin.site.register(Book, BookAdmin)

forms.py

from django import forms

class BookAdminForm(forms.ModelForm):
    additional_field = forms.CharField(max_length=100)

    class Meta:
        model = Book

Next, you'll need to override the save method to check if the "certain option" is selected then handle the extra field appropriately. Inside your class you'll need something like this:

def save(self, *args, **kwargs):
    if self.cleaned_data['certain_field'].value == 'certain option':
        # process additional_field
    # don't forget to call super to save the rest of the form
    super(BookAdminForm, self).save(*arg, **kwargs)

NOTE: This code is untested, but it should point you in the right direction.

It is also possible to override the save_model() method in the ModelAdmin.

Soviut
Might be better to override the admin class's save_model method here, rather than form.save, given that you might need to do something like redirect the user.
Daniel Roseman
in this i am displaying the additional field every time.But I want when there is certain condition only then it is displayed.I am using javascript for this,but want to change .
ha22109
You are going beyond the scope of the django admin then. The whole point of the admin is to instantly have a functional model admin, right out of the box. The javascript route is not a bad solution.
Soviut