这是网站的流程:用户创建一个帐户。然后注册一些地址。那么当他/她想下订单时,他/她必须选择之前注册的地址之一
这是模型:
class User(models.Model):
customer_id = models.AutoField(primary_key=True)
class Address(models.Model):
address_id = models.BigAutoField(primary_key=True,)
address = models.CharField(max_length=500, blank=False,null=False,)
customer = models.ForeignKey(User, on_delete= models.CASCADE)
class Order(models.Model):
order_id = models.BigAutoField(primary_key=True)
customer = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
address = models.ForeignKey(Address, on_delete=models.SET_NULL, null=True)
我的问题的原因是我必须使用formtools (FormWizard)将订购表格分成几页。这是表格:
class NewOrderForm1(forms.Form):
...
class NewOrderForm2(forms.Form):
address = forms.ChoiceField(required=True,choices=??)
name = forms.CharField()
class NewOrderForm3(forms.Form):
...
class NewOrderForm4(forms.Form):
...
如您所见,我有 4 种形式,地址字段是第二种形式。在views.py
我有这个:
class NewOrder(SessionWizardView, LoginRequiredMixin):
form_list = [NewOrderForm1, NewOrderForm2, NewOrderForm3, NewOrderForm4]
def get_template_names(self):
return [TEMPLATES[self.steps.current]] # templates for each form
def done(self, form_list,*context ,**kwargs):
type_form, deliver_info_form, time_form, final_form = form_list # "deliver_info_form" is the second form(NewOrderForm2) in forms.py
address = deliver_info_form.address
name = deliver_info_form.name
customer = self.request.user
order = Order.objets.create(
address=address,
name=name,
customer_id=customer.customer_id,
)
order.save()
return render(self.request, template_name="thanks.html",)
直到第二个表单/模板都可以正常工作。但我无法获得任何地址。
提前谢谢:)