我有以下基类(代码缩短):
class SignupForm(GroupForm):
username = forms.CharField(
label = _("Username"),
max_length = 30,
widget = forms.TextInput()
)
def __init__(self, *args, **kwargs):
super(SignupForm, self).__init__(*args, **kwargs)
if REQUIRED_EMAIL or EMAIL_VERIFICATION or EMAIL_AUTHENTICATION:
self.fields["email"].label = ugettext("Email")
self.fields["email"].required = True
else:
self.fields["email"].label = ugettext("Email (optional)")
self.fields["email"].required = False
def after_signup(self, user, **kwargs):
"""
An extension point for subclasses.
"""
pass
我想要做的是覆盖after_signup()
函数和username
字段,如下所示:
class CompanySignupForm(SignupForm):
#TODO: override fields for company signup form
username = forms.CharField(
label = _("Username TEST"),
max_length = 30,
widget = forms.TextInput()
)
def after_signup(self, user, **kwargs):
"""
An extension point for subclasses.
"""
print str('after_signup is has been overwritten')
我的问题:
只有用户名字段显示所需的行为。永远不会调用after_signup()
函数。而是调用基类after_signup()
的{{1}}函数。我做错了什么?
修改
SignupForm
实例化CompanySignupForm:
the imports:
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _, ugettext
从基类中的函数调用after_signup():
url(r"^signup/$", CompanySignupForm.as_view(), name="acct_signup")
答案 0 :(得分:0)
使用isinstance()
检查实例的类型和.__class__
,以确保您实例化CompanySignupForm
。
此外,您可能希望在__init__
上创建CompanySignupForm
方法,以确保它不仅仅是实例化超类。
注意:仔细阅读你的编辑你不直接调用after_signup基类函数save是对的吗?然后,如果它存在,它将调用它的本地方法after_signup。我将该函数从基类中取出并强制它调用继承的函数
检查版本运行:
signup = CompanySignupForm.as_view()
print signup.__class__
url(r"^signup/$", signup, name="acct_signup")