23

我希望从我的视图中动态更新 ModelForm 的内联 Meta 类。尽管此代码似乎更新了 Meta 类中的排除列表,但 、 等的输出as_p()并未as_ul()反映更新后的 Meta 排除。

然后我假设 html 是在创建 ModelForm 时生成的,而不是在as_*()调用时生成的。有没有办法强制更新 HTML?

这甚至是最好的方法吗?我只是假设这应该有效。

想法?

from django.forms import ModelForm

from testprogram.online_bookings.models import Passenger

class PassengerInfoForm(ModelForm):

    def set_form_excludes(self, exclude_list):
        self.Meta.exclude = excludes_list

    class Meta:
        model = Passenger
        exclude = []
4

4 回答 4

59

Meta 类用于动态构造表单定义 - 因此,当您创建 ModelForm 实例时,不在 exclude 中的字段已添加为新对象的属性。

正常的做法是为每个可能的排除列表设置多个类定义。但是,如果您希望表单本身是动态的,则必须动态创建类定义。就像是:

def get_form(exclude_list):
    class MyForm(ModelForm):
        class Meta:
            model = Passenger
            exclude = exclude_list
    return MyForm

form_class = get_form(('field1', 'field2'))
form = form_class()

更新:我刚刚重新访问了这篇文章,并认为我会发布一些更惯用的方式来处理动态类:

def PassengerForm(exclude_list, *args, **kwargs):
    class MyPassengerForm(ModelForm):
        class Meta:
            model = Passenger
            exclude = exclude_list

        def __init__(self):
            super(MyPassengerForm, self).__init__(*args, **kwargs)

    return MyPassengerForm()

form = PassengerForm(('field1', 'field2'))
于 2008-11-18T00:38:08.247 回答
13

其他方式:

class PassengerInfoForm(ModelForm):
    def __init__(self, *args, **kwargs):
        exclude_list=kwargs.pop('exclude_list', '')

        super(PassengerInfoForm, self).__init__(*args, **kwargs)

        for field in exclude_list:
            del self.fields[field]

    class Meta:
        model = Passenger

form = PassengerInfoForm(exclude_list=['field1', 'field2'])
于 2009-04-01T02:54:06.560 回答
4

类似的方法,有些不同的目标(用于任意模型的通用 ModelForm):

from django.contrib.admin.widgets import AdminDateWidget
from django.forms import ModelForm
from django.db import models

def ModelFormFactory(some_model, *args, **kwargs):
    """
    Create a ModelForm for some_model
    """
    widdict = {}
    # set some widgets for special fields
    for field in some_model._meta.local_fields:
        if type(field) is models.DateField:
            widdict[field.name] = AdminDateWidget()

    class MyModelForm(ModelForm): # I use my personal BaseModelForm as parent
        class Meta:
            model = some_model
            widgets = widdict

    return MyModelForm(*args, **kwargs)
于 2010-10-01T16:02:02.880 回答
3

使用modelform_factory文档):

from django.forms.models import modelform_factory

from testprogram.online_bookings.models import Passenger

exclude = ('field1', 'field2')
CustomForm = modelform_factory(model=Passenger, exclude=exclude)  # generates ModelForm dynamically
custom_form = CustomForm(data=request.POST, ...)  # form instance
于 2015-11-13T02:31:33.093 回答