﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33832	Support M2M validation using signals	ldeluigi	nobody	"MyModel has one ManyToManyField that links to AnotherModel.


For validation purposes I need to check the model after m2m relationships have been set with custom logic based on the db (I can't check it before m2m fields are populated)


At the moment, I'm raising ValidationError inside a signal receiver:

{{{
@receiver(m2m_changed, sender=MyModel.relationship.through)
def validator_from_signal(sender, instance, action, reverse, model, **kwargs):
    if action == 'post_add':
        if reverse and not check_reverse(instance):
            msg = f'AnotherModel {instance.id} violates validation checks.'
            logging.error(msg)
            raise ValidationError(msg)
        elif not reverse and not check(instance):
            msg = f'MyModel {instance.id} violates validation checks.'
            logging.error(msg)
            raise ValidationError(msg)
}}}



It works, thanks to the transaction behaviour which in case of exception rollbacks everything, but if the user submits a wrong MyModel or AnotherModel (because of ManyToMany illegal relationships), in particular with the Admin Form, they get a 500 internal server error. Which isn't very user friendly...


I know this is the expected behaviour, but I'd like the Admin form to handle ValidationError(s) and/or IntegrityError(s) from signal as normal ValidationError. I can't implement this check inside the clean() method:
1. because m2m fields are not populated on the database at that stage
2. because it would only work for the admin form

What do you think?"	New feature	closed	contrib.admin	4.0	Normal	invalid	validation m2m signal manytomany		Unreviewed	0	0	0	0	0	0
