开发者

Django models | get specific columns

开发者 https://www.devze.com 2022-12-27 15:29 出处:网络
Is there a way to filter and get only specific c开发者_Go百科olumns? For example, get all entries with the column first_name.QuerySet.values() or QuerySet.values_list(), e.g.:

Is there a way to filter and get only specific c开发者_Go百科olumns?

For example, get all entries with the column first_name.


QuerySet.values() or QuerySet.values_list(), e.g.:

Entry.objects.values('first_name')


If you want a list of only the values, use:

Entry.objects.values_list('first_name', flat=True)


To only get a column's values from the table but still return an object of that model, use only:

record = Entry.objects.only('first_name')

This will defer all other columns from the model but you can still access them all normally.

record.first_name # already retrieved
record.last_name  # retrieved on call
0

精彩评论

暂无评论...
验证码 换一张
取 消