0

I have a form called shift

here my forms.py

class ShiftForm(forms.ModelForm):

    class Meta:
        model = Shift
        fields = '__all__'

and have createview class for my shift

here my views.py

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

and I already create the form template, like this.

enter image description here

its work and the data was submitted to my database, imagine my database table like this:

#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           |
| ....          | ....          | ....          | ....          | ....       | ....       | ...         |
+---------------+---------------+---------------+---------------+------------+------------+-------------+

my question is how to make it multiple in one form?...

example like this:

enter image description here

so on my database table will look like this in single submit.

#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           |
| ....          | ....          | ....          | ....          | ....       | ....       | ...         |
+---------------+---------------+---------------+---------------+------------+------------+-------------+

on my example above its 4 times submitted in one execute.

thank you!

7
  • What is RollingShift? Commented Jul 30, 2018 at 8:22
  • You could for example use multiple forms (a FormSet) for this. Commented Jul 30, 2018 at 8:23
  • @WillemVanOnsem edited, that was missspelling. Commented Jul 30, 2018 at 8:24
  • @WillemVanOnsem can you give me example for FormSet ? Commented Jul 30, 2018 at 8:24
  • 1
    see the documentation: docs.djangoproject.com/en/2.0/topics/forms/formsets Commented Jul 30, 2018 at 8:25

1 Answer 1

1

In your front end, add an onclick attribute to your save button and call a function to arrange your data in a JSON so they would look like the following:

{
  "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"},
                    .
                    .
                    .
  ]
}

Of course at the end of the function after arranging the data inside this JSON, post it to the correct URL.

In your view you should handle this JSON differently by adding a well written post method:

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)

If you would like to do the process without a json just submit your form and instead of the line:

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

write

 form = request.POST

remember to clean, validate and save the data in the form manually the same as in json, so if you run print(request.POST) you will notice that your data will look like:

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

Of course your QueryDict will look different but if you know how will it look you should be able to deal with it.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, You can access the data submitted through your form using "request.POST". instead of the line "json_data = json.loads(request.body.decode('utf-8'))" use "form = request.POST". If you are confused what to do with it try print(request.POST) to see the submitted data, then you can build an imagination of how you will work with it. This way you just have to submit your form without creating a json. If you need more info, don't hesitate to ask.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.