Django嵌套在线表格 [英] Django Nested Inline Formsets

查看:263
本文介绍了Django嵌套在线表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决问题,但我不知道如何面对这个问题。我需要一些帮助: - )

I'm trying to solve a problem, but I have no idea how to face it. I need some help :-)

我有一个这样的模型层次:

I have a model hierarchy like this:

class Task(models.Model):
    name = models.CharField(max_length=255)
    number_of_steps = models.IntegerField()

class StepGroup(models.Model):
    task = models.ForeignKey(Task)
    init_date = models.DateField()

class Step(models.Model):
    group = models.ForeignKey(StepGroup)
    name = models.CharField(max_length=255)

我必须写一个对话框,我创建了一些步骤组。一个InlineFormSet看起来很方便,但是由于有两个层次的嵌套,我不知道该怎么做。

I must write a dialog where I create a number of Step Groups. An InlineFormSet looks the way to go, but as there are two levels of nesting, I don't know how to do that.

此外,我需要通常的东西形式:错误控制(form.name_of_field.errors),编辑任务等时自动填充,因此手动执行JavaScript和服务器端处理POST请求将会太复杂和容易出错。

Besides, I need the usual stuff with forms: error control (form.name_of_field.errors), autopopulation when editing the Task, etc, so doing it manually with javascript and server-side handling of the POST request would be too complicated and error-prone.

这是我需要表单的方式(每个组中的步骤数量由任务模型中的number_of_steps字段设置):

This is the way I need the form (the number of steps in each group is set by the "number_of_steps" field in the Task model):

+-----------------------------------+
| STEP GROUP 1                      |
|                                   |
| Init date: _____________          |
|                                   |
| Step 1: ________________          |
| Step 2: ________________          |
| Step 3: ________________          |
|                                   |
+-----------------------------------+
| STEP GROUP 2                      |
|                                   |
| Init date: _____________          |
|                                   |
| Step 1: ________________          |
| Step 2: ________________          |
| Step 3: ________________          |
|                                   |
+-----------------------------------+
|                                   |
|             +-------------------+ |
|             | Create step group | |
|             +-------------------+ |
+-----------------------------------+

请,任何类型的帮助,建议或链接将非常感激。

Please, any kind of help, suggestion or link will be very appreciated.

推荐答案

如果一些未来的用户在这里寻找类似的问题,我已经解决了这个 awesome 博客文章: http://yergler.net/blog/2013/09/03/nested-formsets-redux/

In case some future user lands here searching for a similar question, I've solved with this awesome blog post: http://yergler.net/blog/2013/09/03/nested-formsets-redux/

当你看到它解决时,看起来很简单...

Looks so simple when you see it solved...

这篇关于Django嵌套在线表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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