[Answer]-How to get only selected colums and format it in Django from database?

1👍

You can use values_list for this.

0👍

if query is a queryset, then the variable i is an object.

you can get the object attributes like normally with object.attribute syntax (that is, i.name, i.host, etc.)

make a regular html out of the attributes you pull out of the loop.

You can perform a raw sql query like the following example:
first_person = Person.objects.raw(‘SELECT name, address from myapp_person’)
for some model Person

You should definitely checkout the django documentation.
https://docs.djangoproject.com/en/1.4/

👤yassi

0👍

You can use Entry.objects.values('field1','field2','field3') which will return a list dictionary i.e [{'field1':value1,'field2':value2,'field3':value3},..]

Or alternatively you could use Entry.objects.values_list('field1','field2','field3') which will return a list of tuples of values i.e [(value1,value2,value3),..]. In case you are trying to get just one field from the database this can work Entry.objects.values_list('field1',flat=True) the flat parameter will return a list of values instead of list of tuples of values i.e [value1a,value1b,value1c]

Leave a comment