django - I want only Name of engineers from the engineer table -
it out put after print enigeer,here want name of engineer sachin,rahul,altaf
[<engineer: sachin>, <engineer: rahul>, <engineer: altaf>] def enginer(request): engineer=engineer.objects.all() print engineer
an more concise way retrieve names use flat list passing flat=true
values_list
function:
engineer.objects.values_list('name', flat=true)
and return:
[u'sachin', u'rahul', u'altaf']
you can use flat=true
if you're retrieving single column.
however, if still want use engineer objects down road, you'd better of constructing list using list comprehension:
>>> engineers = engineer.objects.all() >>> names = [x.name x in engineers] >>> print names [u'sachin', u'rahul', u'altaf'] >>> engineer in engineers: ... do_something()
Comments
Post a Comment