Django ModelForm - conditional validation -


i need add conditional piece of validation modelform.

below listing model.

listing_types = (     ('event', 'event'),     ('release', 'release') )  class listing(models.model):     title = models.charfield(max_length=255, verbose_name='listing title')     type = models.charfield(max_length=255, choices=listing_types, verbose_name='listing type')     slug = models.slugfield(max_length=100)     content = models.textfield(verbose_name='listing overview')     competition = models.textfield()     date_start = models.datetimefield()     time_start = models.charfield(max_length=255)     date_end = models.datetimefield()     time_end = models.charfield(max_length=255)     pub_date = models.datetimefield('date published', auto_now_add=true)     venue = models.foreignkey(venue)  class listingform(modelform):     date_start = forms.datefield(input_formats=date_input_formats)     date_end = forms.datefield(input_formats=date_input_formats)     class meta:         model = listing 

venue should only required if type == 'event'. if type == 'release', want venue required=false

how can go this?

thanks

first listing.venue needs allow null values

venue = models.foreignkey(venue, blank=true, null=true) 

your modelform needs clean method. following

def clean(self):     cleaned_data = super(listingform, self).clean()     venue = cleaned_data.get("venue")     type = cleaned_data.get("type")      if type == 'event' , not venue:         raise forms.validationerror("a venue required events") 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -