【问题标题】:Issues with Multiple models and templates多个模型和模板的问题
【发布时间】:2012-09-17 09:05:42
【问题描述】:

我正在开发一个包含多个模型的应用程序。在我的 model.py 中的第二个模型中,它由 2-3 列组成。 char 字段,但在我的模板中,当我使用此 {{ form.billing_add }} 时,它不会在浏览器中显示任何文本输入

我的model.py文件是

从 django.db 导入模型 from django.contrib.auth.models 导入用户

class Customer(models.Model):
    user        =models.OneToOneField(User)
    birthday    =models.DateField()
    website     =models.CharField(max_length=50)
    store       =models.CharField(max_length=50)
    welcomemail =models.CharField(max_length=50)

    def __unicode__(self):
             return self.user

class Customer_check_attributes(models.Model):
    customer    = models.ForeignKey(Customer)
    billing_add =models.CharField(max_length=50)
    shipping_add    =models.CharField(max_length=50)
    payment_method  =models.CharField(max_length=50)
    shipping_method =models.CharField(max_length=50)
    reward_points   =models.CharField(max_length=50)

和我的 form.py 文件一样

from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm
from customer_reg.models import Customer,Customer_check_attributes

class Registration_Form(ModelForm):
       first_name  = forms.CharField(label=(u'First Name'))
       last_name   = forms.CharField(label=(u'Last Name'))      
       username   = forms.CharField(label=(u'User Name'))
       email      = forms.EmailField(label=(u'Email Address'))
       password   = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False))

       class Meta:
              model=Customer

              exclude=('user',)

【问题讨论】:

  • 您的表单没有billing_add
  • 我的表单也没有生日和欢迎邮件,但它获得了我在浏览器模型中指定的所需字段,那么为什么这些问题出现在第二个模型中
  • Customer 不知道Customer_check_attributes,因为关系是在Customer_check_attributes 上定义的。因此,表单不能一直跟随到 Customer_check_attributes 字段。
  • 您最好使用普通的Form,设置您需要的所有字段,然后使用cleaned_data,并在表单is_valid() 后自己创建模型。

标签: django django-models django-forms django-templates


【解决方案1】:

您似乎想要一个包含多个模型属性的 html 表单。 因此,方法是为每个模型创建单独的表单,但通过一个视图和模板将其显示在一个页面上,并以相同的方式处理它。

这里是例子

表格

#additional user attributes 
class Registration_Form(ModelForm):
     first_name  = forms.CharField(label=(u'First Name'))
     last_name   = forms.CharField(label=(u'Last Name'))      
     username   = forms.CharField(label=(u'User Name'))
     email      = forms.EmailField(label=(u'Email Address'))
     password   = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False))
     class Meta:
         model = User

class CustomerForm(ModelForm):
     class Meta:
         model = Customer
         exclude=('user',)

class Customer_check_attributesForm(ModelForm):
     class Meta:
         model = Customer_check_attributes
         exclude=('user',)

查看:

def customer_add(request):
    if request.method == 'POST':
         uform = Registration_Form(request.POST)
         cform = CustomerForm(request.POST)
         caform = Customer_check_attributesForm(request.POST)
         if uform.is_valid() :
             ...
             user = uform.save()
         if cform.is_valid() :
             ...
             customer = cform.save()
         if caform.is_valid() :
             ...
             cattr = caform.save()
  else:
      uform = Registration_Form()
      cform = CustomerForm()
      caform = Customer_check_attributesForm()         
  ctx = { 'uform': uform, 'cform':cform, 'caform': caform }
    return render_to_response('add_customer_template.html', ctx,
                context_instance = RequestContext(request))

示例 add_customer_template.html

 <form> 
     {#other rendering logic#}
     {{ uform.as_p }}
     {{ cform.as_p }}
     {{ caform.as_p }}
 </form>

注意:这是一个指导代码。在处理错误和更好的逻辑方面还有改进的余地。

【讨论】:

    猜你喜欢
    • 2012-03-21
    • 2012-11-07
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    相关资源
    最近更新 更多