Django 查询集获取模型字段的名称

Django queryset get names of model fields

我想通过queryset获取Django模型的字段名。

Models.py:

class User1(models.Model):
    types1 = (
        ('a', 'a'),
        ('b', 'b'),
        ('c', 'c'),
    )
    types2 = (
        ('a', 'a'),
        ('b', 'b'),
        ('c', 'c'),
    )
    q1 = models.CharField (
        'Today1',
        max_length=200,
        choices=types1, blank=True, null=True, default='---------'
    )
    q2 = models.CharField (
        'Today2',
        max_length=200,
        choices=types2, blank=True, null=False, default='---------',
    )

queryset 之后,我想得到 [q1,q2] 或类似的东西,但只包含字段名称。

我找到了答案。

[f.name for f in User1._meta.get_fields()]
['id', 'q1', 'q2']