Validation not invoked in custom admin action

Hi,
I have a custom admin action that update the queryset of the selected items in the admin list:

# Django 3.2
def custom_action(self,description="Log it"):
    
    def action_func(modeladmin, request, queryset):
      try:
         _task_date = datetime.datetime.strptime(request.POST['task_date'], "%d/%m/%Y")
         queryset.update(task_date=_task_date)
         messages.info(request, "All selected tasks logged successfully")
           
      except:
         emsg = sys.exc_info()[1]
         messages.error(request,emsg) 
             

    action_func.short_description = description
    return action_func

The issue is the validation that is implemented with the model “Clean” method is never invoked. Note that when saving/updating one instance of the model the validation is processed correctly.

Thanks in advance.

Correct.

From the docs on the update method:

Finally, realize that update() does an update at the SQL level and, thus, does not call any save() methods on your models, nor does it emit the pre_save or post_save signals (which are a consequence of calling Model.save()).

If you want the clean method run on all the rows, you need to process them individually and not as a bulk update.

1 Like

Many Thanks @KenWhitesell