2

每次创建TestForm下面指定的实例时,我都必须用auto_id=True. 这怎么能只在表单类中完成一次呢?任何提示都非常受欢迎。

视图.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)
4

2 回答 2

3
class TestForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(TestForm, self).__init__(*args, **kwargs) 
        self.auto_id = True
    class Meta:
        model = Test
于 2013-11-07T09:50:56.857 回答
2

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

class TestForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(TestForm, self).__init__(auto_id=True, *args, **kwargs)
于 2013-11-07T09:48:28.677 回答