callat-qcd / espressodb

Science database interface using Django as the content manager.
https://espressodb.readthedocs.io
BSD 3-Clause "New" or "Revised" License
8 stars 3 forks source link

[BUG] Form validation for m2m fields #77

Open ckoerber opened 4 years ago

ckoerber commented 4 years ago

Describe the bug

Currently, Base.check_m2m_consistency does not trigger on model forms is_valid method. The form.save() method will not insert data (still consistent), however the error is not captured and causes the app to crash. This extends #72.

To Reproduce

See #76 https://github.com/callat-qcd/espressodb/blob/be3c819a304abb1a2baba4d4569c718cc1d06f5c/tests/espressodb_tests/espressodb_tests/m2mtests/tests.py#L204

Expected behavior

The clean method for forms should raise a ValidationError and not trigger ConsistencyErrors on save.

Additional context

It seems to me that the only way to ensure that consistency errors are captured and validation errors are raised on clean is to update Djangos ModelMultipleChoiceField. The new ModelMultipleChoiceConsistencyField calls the Base class check_m2m_consistency on clean. Additionally, one needs to update the model-field -> form-field register such that the new ModelMultipleChoiceConsistencyField with consistency checks is rendered on default.