Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django form – the same field multiple times

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

'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

>Solution :

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

see more info in Django documentation

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading