Tests also need to be implemented along the way, where applicable, to ensure we are not altering functionality.
Django offers several places to implement validation logic and this has created cases of inconsistent use of the form clean vs model clean methods. This is confusing for ongoing maintenance and in most cases, there is no need to use anything other than the model clean method for the vast majority of business logic we implement. To be clear, this proposal is scoped only to that logic which directly affects the model itself, not any ancillary form control logic which may exist. Centralizing logic in the model clean method will also ensure validation feature parity between the UI and API.
Also: because calling save() directly on a model does not call clean(), we need to be especially vigilant to identify any occurrences where save() is called outside of a form or serializer.
I went through and identified all instances where we need to move validation logic. There is a lot less than I had anticipated. Although we do use the form clean method a lot, most occurrences do not actually have to do with model validation logic.
Here is what I found:
| App | Type | Name | Comments | Action |
|--------|-------------|---------------------------|--------------------------------------------------------------------------------------|---------------------------------------------------------------------|
| dcim | forms | ChildDeviceCSVForm | Setting object parent relationships | Move this logic to model save method |
| dcim | forms | InterfaceBulkEditForm | 1) Validation for tagged vlans & model. 2) Removing tagged vlans in tagged all model | 1) Move to model clean method 2) Move to model save method |
| dcim | forms | InterfaceBulkEditForm | Validating length and length_unit | Move this logic to model clean method |
| dcim | serializers | RackSerializer | Custom unique together validator for the group and facility fields | Investigate if this can actually be moved to the model clean method |
| dcim | serializers | DeviceSerializer | Custom unique together validator for the rack, position, and face fields | Investigate if this can actually be moved to the model clean method |
| dcim | serializers | InterfaceSerializer | Vlan tagging validation | Move this logic to model clean method |
| extras | serializers | ImageAttachmentSerializer | Validating that the parent object exists | Investigate if this can actually be moved to the model clean method |
| ipam | forms | IPAddressForm | Validating IP Address model constraints | Move this logic to model clean method |
| ipam | serializers | VLANGroupSerializer | Custom unique together validator for the name and slug fields | Investigate if this can actually be moved to the model clean method |
| ipam | serializers | VLANSerializer | Custom unique together validator for the name and vid fields | Investigate if this can actually be moved to the model clean method |
Is the project accepting PR's to address these?
@itdependsnetworks In theory, yes, but there are several requirements to consider:
Most helpful comment
Also: because calling
save()directly on a model does not callclean(), we need to be especially vigilant to identify any occurrences wheresave()is called outside of a form or serializer.