How to use custom form fields for model fields in

2019-04-05 22:38发布

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.

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-04-05 23:31
class MyModelForm(forms.ModelForm):
    my_field = MyCustomFormField()

    class Meta:
        model = MyModel

class MyModelAdmin(admin.ModelAdmin):
    form = MyModelForm
查看更多
登录 后发表回答