django-admin change form page validation

Showing 1-7 of 7 messages
django-admin change form page validation Nikhil Verma 3/30/12 8:55 PM
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 :-

INTERVIEW_TYPES = (
       
        ('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

Its admin.py

class InterviewAdmin(admin.ModelAdmin):
    list_display = ('id','title', 'starting_section','ic_number','show_prior_responses')
    raw_id_fields = ('starting_section',)
admin.site.register(Interview, 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 .

Thanks

--
Regards
Nikhil Verma
+91-958-273-3156

Re: django-admin change form page validation dummyman dummyman 3/30/12 9:58 PM
Hi

unless u dont specify blank=True or null = True in modes for d corresponding fields, django will throw an errror if u don fill up the field

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Re: django-admin change form page validation Nikhil Verma 3/30/12 10:04 PM
Hi

I understand that but the problem is its a condition which i have to add if the revisit checkbox is selected and then in interview_type dropdown None option is selected ; after pressing save it should throw an error .

None is an option in the dropdown . It is not blank=True and null =True

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

interview_type will show choices with dropdown 1) None 2) Paired Visit 3) Time Series
Re: django-admin change form page validation dummyman dummyman 3/30/12 10:20 PM
ok. in admin,.py file


override the response_change method

def response_change(self,request,obj)

obj is the handler

so u can check using obj.field_name and take approp action
Re: django-admin change form page validation Nikhil Verma 3/31/12 5:31 AM
Hi

I did not override response_change method but i override clean method by creating form in admin like this :-

class InterviewAdminForm(forms.ModelForm):
    class Meta:
        model = Interview
   
    def clean(self, *args, **kwargs):
        cleaned_data = super(InterviewAdminForm, self).clean(*args, **kwargs)
       
        if self.cleaned_data['interview_type'] == "default" \
        and self.cleaned_data['Revisit'] == True:
            raise forms.ValidationError({'interview_type': ["error message",]})
        else:
            return cleaned_data

It displays the ValidationError message on the admin page but somehow i want show that error just above the interview_type field .
How can i achieve this ?

This method  works pretty fine. I want to know when we use response_change method ?


Thanks in advance .
Re: django-admin change form page validation Tom Evans 4/2/12 2:24 AM
On Sat, Mar 31, 2012 at 4:55 AM, Nikhil Verma <varma.n...@gmail.com> wrote:
> Hi All
>
> How can i apply validation in admin on various fields when they are
> dependent on each other ?
>

There is good advice laid out in the docs for when you need to clean
and validate fields that depend on each other.

https://docs.djangoproject.com/en/1.4/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other

Cheers

Tom

Re: django-admin change form page validation lawgon 3/31/12 10:07 PM
On Sat, 2012-03-31 at 09:25 +0530, Nikhil Verma wrote:
> How can i do this kind validation where fields are dependent on each
> other

https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#adding-custom-validation-to-the-admin

docs are wonderful
--
regards
Kenneth Gonsalves