Python 向django注册添加自定义字段
我正在使用django注册模块并尝试添加自定义字段:Python 向django注册添加自定义字段,python,django,Python,Django,我正在使用django注册模块并尝试添加自定义字段: RegistrationForm.base_fields.update(FarmerForm.base_fields) class CustomRegistrationForm(RegistrationForm): def save(self, profile_callback=None): user = super(CustomRegistrationForm, self).save(profile_callbac
RegistrationForm.base_fields.update(FarmerForm.base_fields)
class CustomRegistrationForm(RegistrationForm):
def save(self, profile_callback=None):
user = super(CustomRegistrationForm, self).save(profile_callback=None)
f = Farmer(user=user)
f.farm = self.cleaned_data['farm']
f.produce = self.cleaned_data['produce']
f.produce_price = self.cleaned_data['produce_price']
f.save()
但出于某种原因,
以下部分:
f = Farmer(user=user)
f.farm = self.cleaned_data['farm']
f.produce = self.cleaned_data['produce']
f.produce_price = self.cleaned_data['produce_price']
f.save()
不执行
我做错了什么
那么,向django注册添加自定义字段的最佳方法是什么?谢谢 您需要调用
self.is\u valid()
才能使用cleaned\u data
那么向django注册添加自定义字段的最佳方法是什么?谢谢@用户2769816只需在调用super后将它们添加到\uuuuu init\uuuuu
中即可。示例:self.fields['field\u name']=forms.TextField()
如何确保验证FarmerForm?@user2769816 karthikr已经回答了这个问题,请参考django文档中有关使用表单的内容,了解应该如何进行验证。