python - Help with validation in Models and Forms -
i have few questions validation in models , forms. me out these:
where should validation done? should in model or form? right way go have validators in form , constraints in mode?
what difference between writing 'clean_' method in form , writing validator? i've seen people put validation checks in 'clean_' method.
in request i'm handling, have param in url string called 'alive'. 1 or 0. correct way of defining in form? need validate number , can 1 or 0. right way?
alive = models.integerfield(null=false, max_value=1, min_value=0)
how define default value field i.e. if parameter isn't passed, default 0 (false).
i don't have form on client side. i'm using django form validate js post request.
in 1 of model fields need store screen resolution in format 1234x4321. should declare charfield
add regular expression validation in both model , form? examples of regular expression validations helpful.
thanks.
the validation should done on form, not model. however, if using modelforms, makes lot of sense, inherit of validation rules models (those specific database, maximum_field length, database field type, if can left blank).
the default value of field should passed constructor:
form = someform(initial={'alive' : 0})
although in case, appears if values can obtained 0 , one, make sense use booleanfield
instead(tand in case default false).
in case of resolutions create mapping between possible resolution , arbitrary value.
resolutions = ( ("1","800x600"), ("2","1024x768"), ..... )
and pass model:
resolutions = models.charfield(resolutions, max_length=1)
so user gets select field corresponding options , values. on other hand, if need user insert him/herself, using 2 fields (one width, height) easier validating user input.
so can define method model:
def get_resolution(self): return "%sx%s" % (self.width, self.height)
Comments
Post a Comment