Friday, March 30, 2012

django-admin change form page validation

Hi All

How can i apply validation in admin on various fields when they are dependent on each other ?

e.g. Let say in i have a  Field A(BooleanField)  and Field B (CharField) what i want to do is if in admin user select the Field A(checkbox) and does not enter anything in Field B
and if he tries to save ,it should throw an error like a normal blank=False gives. So how can i do this kind of validation in admin .

E.g  Use Case

I have a table having the following structure :-

        ('default', 'None'),
        ('Paired Visit','Paired Visit'),
        ('Time Series', 'Time Series'),

class Interview(models.Model):
    ic_number              = models.CharField(verbose_name ="Visit Configuration Number",max_length=20,unique=True,null =True,blank=True)
    ic_description         = models.TextField(verbose_name ="Visit Configuration Description",null = True,blank=True)
    title                  = models.CharField(verbose_name ="Visit Configuration Title",max_length=80,unique=True)
    starting_section       = models.ForeignKey(Section)
    interview_type         = models.CharField(verbose_name = "Mapped Visit",choices=CHOICES.INTERVIEW_TYPES, max_length=80, default="Time Series")
    select_rating          = models.CharField(choices=CHOICES.QUESTION_RATING, max_length=80, default="Select Rating")
    view_notes             = models.CharField(choices=CHOICES.VIEW_NOTES, max_length=80, default="Display Notes")
     revisit                = models.BooleanField(default=False)   
.....and so on ......
    class Meta:
        verbose_name = 'Visit Configuration'
        verbose_name_plural = 'Visit Configurations'
       # ordering = ('rpn_number',)
    def __unicode__(self):
        return self.title


class InterviewAdmin(admin.ModelAdmin):
    list_display = ('id','title', 'starting_section','ic_number','show_prior_responses')
    raw_id_fields = ('starting_section',), InterviewAdmin)

In admin , If i select the checkbox of revisit and in the field interview_type(which will show a dropdown having choices None,Paired Visit , Time Series) if a User has selected None from that dropdown and then press save button it should throw me an error like a normal blank=False shows, saying "This field is required"

How can i do this kind validation where fields are dependent on each other ? 

Please Ignore syntax error is any .


Nikhil Verma

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment