Creating multiple objects with one request in Django and Django Rest Framework

Init the serializer with many=True

In your implementation this is really easy to accomplish:

serialized = MovieTicketSerializer(data=request.data, many=True)

Data is no single object but an array of objects.

Your infos suggest that you need to transform request.data to make those multiple objects (all the same data just different seat number). Right?

anyways:

see: How do I create multiple model instances with Django Rest Framework?

EDIT:

here the info in the drf docu: http://www.django-rest-framework.org/api-guide/serializers/#dealing-with-multiple-objects

(highly suggest to read the drf docs from top to bottom and just playing around with it, before coding your first real implementation. there are many ways to use drf, and knowing all of them leads to better decisions)

EDIT 2 (after question update):

You could send this JSON from the client (see below), or create this format from the current JSON the client sends in your buy_ticket(request) method before you call MovieTicketSerializer(...,many=True):

[
    {
        "purchased_at": null,
        "qrcode": null,
        "qrcode_data": "",
        "show": 11,
        "seat": 106,
        "user": 34
    },
    {
        "purchased_at": null,
        "qrcode": null,
        "qrcode_data": "",
        "show": 11,
        "seat": 219,
        "user": 34
    }
]

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)