如何在Django管理员中使用模型字段的自定义表单字段? [英] How to use custom form fields for model fields in the Django admin?

查看:174
本文介绍了如何在Django管理员中使用模型字段的自定义表单字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让Django管理员使用自定义表单字段来输入某些字段。

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.

我想这可以用某种方式使用自定义管理表单a href =http://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin =noreferrer> http://docs.djangoproject .com / en / dev / ref / contrib / admin /#添加自定义验证到管理员),但是我不知道该怎么做。

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.

推荐答案

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

    class Meta:
        model = MyModel

class MyModelAdmin(admin.ModelAdmin):
    form = MyModelForm

这篇关于如何在Django管理员中使用模型字段的自定义表单字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆