python - What is the best practice to validate a Django DateField at the model level as particular day of the week? -
i have model:
class mymodel(models.model): user = models.foreignkey(user) week = models.datefield() my app being built around "weeks ending on saturday", week field accept days saturday.
i'd validation happen in model rather in form, i'm going have data input without form in circumstances.
i understand overriding model's save() method add full_clean() way go, i'm confused best practice here.
what's dryest way?
i go providing validator, if need clean db when inputting data without form.
if input datetime.date, can use mydate.weekday() day of week. should '5' saturday.
something like:
def validate_saturday(value): if not value.week.weekday == 5: raise validationerror("date should saturday.") an alternative might specify week number in models instead of datefield.
Comments
Post a Comment