Django 4.0 Form Validation scenarios with Custom Field Types in Admin Interface
I'm currently working on a Django 4.0 project and I've created a custom form field that inherits from `forms.Field`. However, when I try to use this custom field in the Django admin interface, the validation seems to be bypassed, and I'm noticing that invalid data is being saved to the database without raising any errors. Here's a simplified version of my custom field: ```python from django import forms class MyCustomField(forms.Field): def to_python(self, value): if not value: raise forms.ValidationError('This field want to be empty.') # Assuming the field should only accept integers try: return int(value) except ValueError: raise forms.ValidationError('Enter a valid integer.') def widget_attrs(self, widget): attrs = super().widget_attrs(widget) attrs['class'] = 'my-custom-class' return attrs ``` I've registered this field in my admin model like this: ```python from django.contrib import admin from .models import MyModel @admin.register(MyModel) class MyModelAdmin(admin.ModelAdmin): form = MyCustomForm class MyCustomForm(forms.ModelForm): my_field = MyCustomField() class Meta: model = MyModel fields = '__all__' ``` In the admin interface, the field appears as expected, but when I submit the form with invalid data (like a string instead of an integer), it seems to accept it and save it to the database without raising the validation behavior. I've tried overriding `clean()` method in my form too, but it didn't help either: ```python class MyCustomForm(forms.ModelForm): my_field = MyCustomField() class Meta: model = MyModel fields = '__all__' def clean_my_field(self): value = self.cleaned_data['my_field'] if not isinstance(value, int): raise forms.ValidationError('Invalid value for my_field.') return value ``` Despite this, the errors are not showing in the admin, and the invalid data is still being saved. Could someone guide to understand what I might be missing? Is there something specific about how custom fields interact with the admin interface in Django that I need to be aware of? I'd really appreciate any guidance on this.