Home > front end >  Django form - the same field multiple times
Django form - the same field multiple times

Time:09-12

how can I process a form with a field:

order = ModelChoiceField(
    required=False,
    queryset=OrderOd.objects.filter(Q(status='DN') | Q(status='DI')),
    widget=Select(
        attrs={
            "class": "form-select form-select-md form-select-solid",
            "data-control": "select2",
            "data-multiple": "true",
            "multiple": "multiple",
            "data-placeholder": _("Vyberte objednávku ..."),
            "id": 'order'
        }
    )
)

In front-end, I can select multiple orders (looks like pills/tags) and in the request sent to the server it looks like this:

movement: f2b7c234-fbdb-4059-bcb6-8ada46cef72c
account: dbabefb7-f053-4edf-a2e3-787bf6bfc371
date: 2022-09-12
order: eb2fc726-3e97-4af2-a8b2-08f20771cfef
order: 8398925b-fca6-4b25-8c48-e12940a5b5c3
order: bfa35391-5cf8-4ed8-8c44-a797da875cb4
order: 07be93ac-20b3-459c-8038-c8b023db6d66

When I inspect self.data, I got

'order': ['eb2fc726-3e97-4af2-a8b2-08f20771cfef', '8398925b-fca6-4b25-8c48-e12940a5b5c3', 'bfa35391-5cf8-4ed8-8c44-a797da875cb4', '07be93ac-20b3-459c-8038-c8b023db6d66'],

but when I check the output of logger.info(self.data['order']), it gives me only the first UUID.

[INFO] form.py 123: 07be93ac-20b3-459c-8038-c8b023db6d66

What I need is to access all UUIDs in the array (order) and process them instance by instance.

Any idea, how to do it? Thanks

CodePudding user response:

You can use self.data.getlist('order') to return the data in the array form.

see more info in Django documentation

  • Related