Django CreateView-如何调用表单类__init__? [英] Django CreateView - How do I call form class __init__?
本文介绍了Django CreateView-如何调用表单类__init__?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个django创建视图,我想调用表单类的 __ init __
,我不知道该怎么做。
I have a django create view and I want to call the __init__
of the form class and I don't know how to do that.
class PersonCreateView(CreateView):
model = Person
form_class = PersonForm
在表单类中,我做了一些逻辑来重新定义一些组合的查询集。我的问题是我不知道如何调用 PersonForm
的 __ init __
方法或任何其他方法
In the form class I made some logic to redefine the queryset of some combos. My problem is that I don't know how to call the __init__
method, or any other method, of the PersonForm
在此先感谢您的帮助。
推荐答案
您不应不要自己叫它。您可以覆盖 get_form_kwargs
提供额外的参数以传递给表单实例化。
You shouldn't call it yourself. You can override get_form_kwargs
to provide extra arguments to pass to the form instantiation.
class PersonCreateView(CreateView):
model = Person
form_class = PersonForm
def get_form_kwargs(self, *args, **kwargs):
form_kwargs = super(PersonCreateView, self).get_form_kwargs(*args, **kwargs)
form_kwargs['my_extra_queryset_param'] = get_my_extra_queryset()
return form_kwargs
这篇关于Django CreateView-如何调用表单类__init__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文