使用Django生成和提交动态数量的表单形式的对象 [英] Generating and submitting a dynamic number of objects in a form with Django

查看:85
本文介绍了使用Django生成和提交动态数量的表单形式的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用Django更新单个表单中的动态数量的对象,我想知道最好的方式是做什么。类似情况的一个例子可能会有所帮助。

I want to be able to update a dynamic number of objects within a single form using Django and I'm wondering what the best way to do this would be. An example of a similar situation may help.

型号:

class Customer(Model.models):
    name = models.CharField(max_length=100)
    active = models.BooleanField()

表单(我知道我正在混合视图和模板代码,这不工作,但这是一个一般的想法,该表单应该做什么):

Form (I know I'm mixing view and template code here which doesn't work but this is a general idea for what the form is supposed to do):

customers = Customer.objects.all()
for c in customers:
    print <li> {{ c.name }} <input type="checkbox" value="{{ c.active }}" name="?" />

我将如何提交这些对象的列表?最好的办法是将客户的id附加到每个行中,然后根据id进行处理?是否有提交元组列表的机制?什么是理想的解决方案?

How would I go about submitting a list of these objects? Would the best bet be to attach the id of the customer into each 'row' and then process based on the id? Is there a mechanism for submitting a list of tuples? What would be the ideal solution?

推荐答案

Formsets

另外,直接模型生成的表单的等价物是模型表单

Also, the equivalent for forms generated directly models are model formsets.

这篇关于使用Django生成和提交动态数量的表单形式的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆