42👍
✅
You have an indentation issue. Your clean
method is indented within the form’s Meta class. Move it back one level. Also, ensure that the return
statement is indented within the method.
72👍
Usually you just want to define a clean() method on the model itself.
https://docs.djangoproject.com/en/2.1/ref/models/instances/#validating-objects
from django.core.exceptions import ValidationError
class Lecture(models.Model):
topic = models.CharField(max_length=100)
speaker = models.CharField(max_length=100)
start_date = models.DateField()
end_date = models.DateField()
def clean(self):
if self.start_date > self.end_date::
raise ValidationError("Dates are incorrect")
Something like that will work in the django admin without any need to create a form class.
👤aris
- [Django]-Composite primary key in django
- [Django]-Django queryset filter – Q() | VS __in
- [Django]-How to put comments in Django templates?
0👍
def clean(self):
if self.database_name != '111':
raise ValidationError("You can use only letters ,numbers and
underscores.")
This clean method works perfect for me in django admin
- [Django]-Django MEDIA_URL and MEDIA_ROOT
- [Django]-Django – how to detect test environment (check / determine if tests are being run)
- [Django]-ImportError: No module named virtualenv
Source:stackexchange.com