问题描述
在 Django 管理员中,我想覆盖并实现我自己的模型表单(例如发票模型).
In Django admin I want to override and implement my own form for a model (e.g. Invoice model).
我希望发票表单具有客户名称、产品名称的自动填充字段,并且我还希望进行自定义验证(例如客户的信用额度).如何覆盖 Django 管理员提供的默认表单并实现我自己的表单?
I want the invoice form to have auto-fill fields for customer name, product name and I also want to do custom validation (such as credit limit for a customer). How can I override the default form provided by Django admin and implement my own?
我是 Django 的新手,感谢您的指点.
I am new to Django, I appreciate any pointers.
推荐答案
您可以通过将 ModelAdmin 的 form 属性设置为您自己的表单类来覆盖 django 的内置 admin 的表单.见:
You can override forms for django's built-in admin by setting form attribute of ModelAdmin to your own form class. See:
- https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form
- https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin
也可以覆盖表单模板 - 看看 https://docs.djangoproject.com/en/dev/ref/contrib/admin/#custom-template-options
It's also possible to override form template - have a look at https://docs.djangoproject.com/en/dev/ref/contrib/admin/#custom-template-options
如果您专门寻找自动完成功能,我可以推荐 https://github.com/crucialfelix/django-ajax-selects
If you're looking specifically for autocomplete I can recommend https://github.com/crucialfelix/django-ajax-selects
这篇关于覆盖 Django 管理中的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!