Jas*_*ebb 89 python django django-forms
是否有可能ModelForm在django中包含多个模型?我正在尝试创建个人资料编辑表单.所以我需要包含User模型和 UserProfile模型中的一些字段.目前我正在使用这样的2个表单
class UserEditForm(ModelForm):
class Meta:
model = User
fields = ("first_name", "last_name")
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
fields = ("middle_name", "home_phone", "work_phone", "cell_phone")
Run Code Online (Sandbox Code Playgroud)
有没有办法将这些整合到一个表单中,或者我只需要创建一个表单并处理数据库加载并保存自己?
您可以尝试使用这段代码:
class CombinedFormBase(forms.Form):
form_classes = []
def __init__(self, *args, **kwargs):
super(CombinedFormBase, self).__init__(*args, **kwargs)
for f in self.form_classes:
name = f.__name__.lower()
setattr(self, name, f(*args, **kwargs))
form = getattr(self, name)
self.fields.update(form.fields)
self.initial.update(form.initial)
def is_valid(self):
isValid = True
for f in self.form_classes:
name = f.__name__.lower()
form = getattr(self, name)
if not form.is_valid():
isValid = False
# is_valid will trigger clean method
# so it should be called after all other forms is_valid are called
# otherwise clean_data will be empty
if not super(CombinedFormBase, self).is_valid() :
isValid = False
for f in self.form_classes:
name = f.__name__.lower()
form = getattr(self, name)
self.errors.update(form.errors)
return isValid
def clean(self):
cleaned_data = super(CombinedFormBase, self).clean()
for f in self.form_classes:
name = f.__name__.lower()
form = getattr(self, name)
cleaned_data.update(form.cleaned_data)
return cleaned_data
Run Code Online (Sandbox Code Playgroud)
用法示例:
class ConsumerRegistrationForm(CombinedFormBase):
form_classes = [RegistrationForm, ConsumerProfileForm]
class RegisterView(FormView):
template_name = "register.html"
form_class = ConsumerRegistrationForm
def form_valid(self, form):
# some actions...
return redirect(self.get_success_url())
Run Code Online (Sandbox Code Playgroud)
我在我的项目中使用了django betterforms的MultiForm 和 MultiModelForm。不过,代码还可以改进。例如,它依赖于 django.6,3.+ 不支持它,但所有这些都可以轻松修复
这个问题在 StackOverflow 中已经出现了好几次 ,所以我认为是时候找到一种标准化的方法来解决这个问题了。
| 归档时间: |
|
| 查看次数: |
58085 次 |
| 最近记录: |