Python/Django: Creating a simpler list from values_list()

后端 未结 4 1777
深忆病人
深忆病人 2020-12-25 11:24

Consider:

>>>jr.operators.values_list(\'id\')
[(1,), (2,), (3,)]

How does one simplify further to:

[\'1\', \'2\',          


        
4条回答
  •  盖世英雄少女心
    2020-12-25 12:11

    Use the flat=True construct of the django queryset: https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.values_list

    From the example in the docs:

    >>> Entry.objects.values_list('id', flat=True).order_by('id')
    [1, 2, 3, ...]
    

提交回复
热议问题