我有一个用 django 处理的大表格。我的模型如下
class MerchantContact(models.Model):
merchant = models.OneToOneField(MerchantProfile, related_name="merchant_contact")
ContactUsAddressLine1 =models.CharField(max_length=16)
ContactUsAddressLine2 =models.CharField(max_length=16)
ContactUsAddressCity =models.CharField(max_length=16)
ContactUsAddressState =models.CharField(max_length=4)
ContactUsAddressZip=models.CharField(max_length=16)
ContactUsPhone=models.CharField(max_length=16)
ContactUsAddressFax=models.CharField(max_length=16)
ContactUsEmail=models.CharField(max_length=64)
ContactUsBusinessHours=models.CharField(max_length=256)
我使用以下逻辑来填充表单
def merchantAccountInfo(request):
#check if data is already there or not
usr=UserProfile.objects.get(user_id=request.user.id)
merchant=MerchantProfile.objects.get(user_id=usr.id)
try:
mc=MerchantContact.objects.get(merchant_id=merchant.id)
formdata=MerchantContactForm(
{'id':mc.id,
'ContactUsAddressLine1':mc.ContactUsAddressLine1,
'ContactUsAddressLine2':mc.ContactUsAddressLine2,
'ContactUsAddressCity':mc.ContactUsAddressCity,
'ContactUsAddressState':mc.ContactUsAddressState,
'ContactUsAddressZip':mc.ContactUsAddressZip,
'ContactUsPhone':mc.ContactUsPhone,
'ContactUsAddressFax':mc.ContactUsAddressFax,
'ContactUsEmail':mc.ContactUsEmail,
'ContactUsBusinessHours':mc.ContactUsBusinessHours
}
)
except MerchantContact.DoesNotExist:
formdata=MerchantContactForm()
return render_to_response('account_info.html',locals(),context_instance=RequestContext(request))
上面的代码运行良好,但我担心表单可能太大,有 30 多个字段。内联传递每个字段将是太乏味的方式..
还有其他节省时间和代码行的替代方法吗?