动态更新ModelForm的Meta类 [英] Dynamically update ModelForm's Meta class

查看:118
本文介绍了动态更新ModelForm的Meta类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从我的视图动态更新一个ModelForm的内联元类。虽然此代码似乎更新了Meta类中的排除列表,但是 as_p() as_ul()等等不反映更新的元数据排除。



我假设当ModelForm被创建时,html是生成的,而不是当 as _ *( )被调用。有没有办法强制更新HTML?



这甚至是最好的方式吗?我只是假设这个应该工作。



想法?

 code> 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 = []


解决方案

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)

返回MyPassengerForm()

form = PassengerForm(('field1','field2'))


I am hoping to dynamically update a ModelForm's inline Meta class from my view. Although this code seems to update the exclude list in the Meta class, the output from as_p(), as_ul(), etc does not reflect the updated Meta exclude.

I assume then that the html is generated when the ModelForm is created not when the as_*() is called. Is there a way to force the update of the HTML?

Is this even the best way to do it? I just assumed this should work.

Thoughts?

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 = []

解决方案

The Meta class is used to dynamically construct the form definition - so by the time you've created the ModelForm instance, the fields not in the exclude have already been added as the new object's attributes.

The normal way to do it would be to just have multiple class definitions for each possible exclude list. But if you want the form itself to be dynamic, you'll have to create a class definition on the fly. Something like:

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()

UPDATE: I just revisited this post and thought I'd post a little more idiomatic way to handle a dynamic 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'))

这篇关于动态更新ModelForm的Meta类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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