In my django app, I have these models
class Lesson(models.Model):
name = models.CharField(max_length=200)
course = models.ForeignKey(Course)
...
class Course(models.Model):
title = models.CharField(max_length=200)
...
I use two forms to get/create a Course instance in my Edit_Lesson template.In one choice_form I use a ModelChoiceField to create a dropdown list in the template.In the other course_name_form I provide a textfield by using CharField where the user can enter a course name.
I need to do the validation for these.If initially there are no courses ie Course.objects.all() is an empty list,the drop down list in choice_form will not have any items for the user to select from.Then,if user forgets to enter a name in the textfield of course_name_form,there wont be any course instance selected/created.When this happens I would like to show the user an error message asking him to input a course name.
I think ,I won’t be able to write the validation code in the forms.Should I create a error_list=[] in my view and put the error in it and display it?This means ,the validation happens in view.I am not sure if this is the right way
please advise
You can override the clean() method of your model forms to perform validation that requires knowledge of a broader scope.
In your case, you might do