views:

24

answers:

1

Hay guys, i want to use something like this

users = User.objects.all()

but i only want to return a couple of fields for each result, say 'name' and 'email'. This data is going t be turned into JSON data, and some fields in my model are sensitive.

How would i do this in django?

+2  A: 

Use values or values_list:

>>> User.objects.values('name', 'email')
[{'name': 'Daniel', 'email':'[email protected]'}, ...]

>>> User.objects.values_list('name', 'email')
[['Daniel', '[email protected]'], ...]

See the documentation.

Daniel Roseman