Saturday, March 31, 2012

Re: django-admin change form page validation

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 .
On Sat, Mar 31, 2012 at 10:50 AM, dummyman dummyman <tempovan@gmail.com> wrote:
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


On Sat, Mar 31, 2012 at 10:34 AM, Nikhil Verma <varma.nikhil22@gmail.com> wrote:
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


On Sat, Mar 31, 2012 at 10:28 AM, dummyman dummyman <tempovan@gmail.com> wrote:
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

On Sat, Mar 31, 2012 at 9:25 AM, Nikhil Verma <varma.nikhil22@gmail.com> wrote:
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

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

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



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

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

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



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

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

No comments:

Post a Comment