我正在努力弄清楚如何最好地解决这个问题。
我有两个模型需要在表单向导中的一个页面上表示:
class BookingItem(models.Model):
assignedChildren = models.ManyToManyField('PlatformUserChildren', related_name = "childIDs", null=True)
quantity = models.PositiveSmallIntegerField(max_length=2,blank=True, null=True)
class PlatformUserChildren(models.Model):
child_firstname = models.CharField('Childs first name', max_length=30,blank=True, null=True)
模型在页面上呈现时的关系不是一对一的。它由数量属性控制,因此可能会有超过在页面上显示的PlatformUserChildren对象的BookingItems(例如4个BookingItems和2个PlatformUserChildren对象)。我根据数量多次遍历每个对象。 我还需要根据当前登录用户绑定到PlatformChildUser模型的查询集。
我的问题:如何在表单向导的第一页上最好地展示这两个模型?
我目前的(相当普通的)代码如下:
forms.py
class checkout_PlatformUserChildren(forms.ModelForm):
#activity_id = forms.IntegerField()
class Meta:
model = PlatformUserChildren
fields = ('child_age','child_firstname')
class Meta:
model = PlatformUserChildren
fields = ('child_age','child_firstname')
widgets = {
'child_firstname': SelectMultiple(attrs={'class': 'form-control',}),
'child_age' : TextInput(attrs={'class': 'form-control',}),
}
checkout_PlatformUserChildrenFormSet = modelformset_factory(
PlatformUserChildren,
form = checkout_PlatformUserChildren,
fields=('child_firstname', 'child_age'),
extra=1, max_num=5, can_delete=True)
views.py(已完成方法未显示) 注意:getUser是一个当前正在运行的外部函数
checkoutForms = [
("assign_child", checkout_PlatformUserChildrenFormSet),
("address_information", addressInfo),
]
checkoutTemplates = {
"assign_child": "checkout/assign_child.html",
"address_information": "checkout/address_information.html",
}
class checkout(SessionWizardView):
def get_form_instance(self, step):
currentUser = getUser(self.request.user.id)
if step == 'assign_child':
self.instance = currentUser
return self.instance