代码之家  ›  专栏  ›  技术社区  ›  Dicky Raambo

Django 1.11表单一次提交多个数据

  •  0
  • Dicky Raambo  · 技术社区  · 6 年前

    我有张表格叫 shift

    这是我的 forms.py

    class ShiftForm(forms.ModelForm):
    
        class Meta:
            model = Shift
            fields = '__all__'
    

    并拥有 createview 我的班级

    这是我的 views.py

    class ShiftCreateView(CreateView):
        fields = ('start', 'end', 'off_start', 'off_end', 'shift', 'employee')
        model = models.Shift
    

    enter image description here

    它的工作和数据被提交到我的数据库,想象一下我的数据库表是这样的:

    #table shift
    +---------------+---------------+---------------+---------------+------------+------------+-------------+
    | start         | end           | off_start     | off_end       | time       | user_id    | shift_id    |
    +---------------+---------------+---------------+---------------+------------+------------+-------------+
    | 2018-01-01    | 2018-01-05    | 2018-01-06    | 2018-01-07    | 07:00      | 1          | 1           |
    | ....          | ....          | ....          | ....          | ....       | ....       | ...         |
    +---------------+---------------+---------------+---------------+------------+------------+-------------+
    

    我的问题是如何使它在一种形式的多重?。。。

    例如:

    enter image description here

    所以在我的数据库表上,在单次提交中看起来是这样的。

    #table shift
    +---------------+---------------+---------------+---------------+------------+------------+-------------+
    | start         | end           | off_start     | off_end       | time       | user_id    | shift_id    |
    +---------------+---------------+---------------+---------------+------------+------------+-------------+
    | 2018-01-01    | 2018-01-05    | 2018-01-06    | 2018-01-07    | 07:00      | 1          | 1           |
    | 2018-01-01    | 2018-01-05    | 2018-01-06    | 2018-01-07    | 07:00      | 1          | 2           |
    | 2018-01-01    | 2018-01-05    | 2018-01-06    | 2018-01-07    | 07:00      | 1          | 3           |
    | 2018-01-01    | 2018-01-05    | 2018-01-06    | 2018-01-07    | 07:00      | 1          | 4           |
    | ....          | ....          | ....          | ....          | ....       | ....       | ...         |
    +---------------+---------------+---------------+---------------+------------+------------+-------------+
    

    谢谢您!

    1 回复  |  直到 6 年前
        1
  •  1
  •   Ta.Da    6 年前

    {
      "employee": "xyz" ,
      "weeks_schedule" :[
        {"data_week": "value_1", "data_off_week": "value_2", "shift": "value_3"},
        {"data_week": "value_4", "data_off_week": "value_5", "shift": "value_6"},
                        .
                        .
                        .
      ]
    }
    

    当然,在函数结束时,在整理完JSON中的数据后,将其发布到正确的URL。

    class ShiftCreateView(CreateView):
    
        def post(self, request, *args, **kwargs):
            json_data = json.loads(request.body.decode('utf-8'))
            employee = json_data["employee"]
            for week in json_data["weeks_schedule"]:
                # Code for saving in the database here
            return _(some_http_response)
    

    如果您想在不使用json的情况下执行此过程,只需提交表单,而不是行:

     json_data = json.loads(request.body.decode('utf-8'))
    

     form = request.POST
    

    记住手动清理、验证和保存表单中的数据,就像在json中一样,因此如果您运行 打印(请求.POST)

    <QueryDict: {'employee': 'xyz', 'off_week': 'xyz_2' ......}>
    

    当然,你的问题看起来会有所不同,但如果你知道它看起来如何,你应该能够处理它。