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

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -