How to use custom form fields for model fields in the Django admin

djangodjango-admindjango-forms

I want to have the Django admin use a custom form field for the input of some fields.

Until now I made a custom model field that has the form field bound to it which works great but it introduces an unecessary model field that does not add any benefit.

I guess it can be done somehow using a custom admin form (see http://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin) but I couldn't figure out how to do it.

Best Answer

class MyModelForm(forms.ModelForm):
    my_field = MyCustomFormField()

    class Meta:
        model = MyModel

class MyModelAdmin(admin.ModelAdmin):
    form = MyModelForm