django:更改基于ModelForm的表单类的auto_id

时间:2013-11-07 09:28:24

标签: python django django-forms

每次创建下面指定的TestForm实例时,我都必须使用auto_id=True覆盖标准ID格式。如何只在表单类中执行一次?任何提示都非常受欢迎。

views.py

from django.forms import ModelForm
from models import Test

class TestForm(ModelForm):
    class Meta:
        model = Test

def test(request):
    form = TestForm(auto_id=True)

2 个答案:

答案 0 :(得分:3)

class TestForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(TestForm, self).__init__(*args, **kwargs) 
        self.auto_id = True
    class Meta:
        model = Test

答案 1 :(得分:2)

您可以在构造函数中覆盖参数:

class TestForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(TestForm, self).__init__(auto_id=True, *args, **kwargs)