Django equivalent of PHP's form value array/associative array

前端 未结 3 1080
甜味超标
甜味超标 2020-12-01 00:36

In PHP, I would do this to get name as an array.



         


        
3条回答
  •  粉色の甜心
    2020-12-01 01:33

    Sorry for digging this up, but Django has an utils.datastructures.DotExpandedDict. Here's a piece of it's docs:

    >>> d = DotExpandedDict({'person.1.firstname': ['Simon'], \
            'person.1.lastname': ['Willison'], \
            'person.2.firstname': ['Adrian'], \
            'person.2.lastname': ['Holovaty']})
    >>> d
    {'person': {'1': {'lastname': ['Willison'], 'firstname': ['Simon']}, '2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']}}}
    

    The only difference being you use dot's instead of brackets.

    EDIT: This mechanism was replaced by form prefixes, but here's the old code you can drop in your app if you still want to use this concept: https://gist.github.com/grzes/73142ed99dc8ad6ac4fc9fb9f4e87d60

提交回复
热议问题