Reputation: 447
I have defined a custom save method. In this method i want to raise some error if one of the field is empty.
def save(self, *args, **kwargs):
# do some operation and end up getting self.iot_boxes
# this model field (iot_boxes) can be a list full of values or an empty list
logger.info(f"Selected IOT boxes to be updated {self.iot_boxes}")
super().save(*args, **kwargs)
Initially iot_boxes
may or may not be empty
or after some operation it can end up empty during operation in the save()
method.
How can i display some kind of error on the admin page when the self.iot_boxes
comes out as empty.
Upvotes: 0
Views: 987
Reputation: 1869
It is not a good solution to make validation in save method.
Most views (and specially in django admin) cannot handle a ValidationError in the save method. Your user will get a 500 error...
You have to make this validation in the form and raise ValidationError there. Then call save method only if the model form data is 'ok for saving'
Upvotes: 1