I have enabled list editable on one of my django admin to allow user update the min and max value. I have set a validation in place to make sure that min is not greater than max. The validation works as expected. However, I am not really happy with the way the message is displayed.
So currently as seeing in the screenshot, the error message appears right above the field which messes up the alignment of the table. The column with error becomes wider than the others and it’s not even highlighted with red.
What I am trying to achieve instead is to show the error on the top of the page like
Please correct the errors below.
Min value cannot be greater than max value
and simply have the errored field highlighted with red border.
what is the best way to achieve this?
I tried overriding the is_valid
function in my CustomModelForm
but I can’t add the messages there since I don’t have access to the request
object. Anything I tried to pass the request object was in vain.
class CustomModelForm(forms.ModelForm):
class Meta:
model = MyModel
fields = '__all__'
def is_valid(self):
if 'min_quantity' in self._errors:
# messages.error(self.request, self._errors["min_quantity"])
print("Handling a specific validation error for 'min'")
del self._errors['min_quantity']
super().is_valid()
Once I am able to set the django messages, what template should I extend to setup highlighting the errored field?