Wednesday, February 11, 2015

How To Customize Django QuerySet values() Method

from mypro.models import Building
from pprint import pprint

a)
Query all the fields of the model class 'Building'

b = Building.objects.filter(id='1015445')
pprint (b.values()[0])


b)
Get selected fields of the model class 'Building'

fields=[f.attname for f in Building._meta.fields]
b = Building.objects.filter(id='1015445')

pprint (b.values(*fields)[0])
len(b.values(*fields)[0])


c)

Include fields from another model class via Foreignkey relation

cust_fields = [f.attname for f in Building._meta.fields]
cust_fields.append('nonuse__description') <--- b="">
b = Building.objects.filter(id='1015445')

pprint (b.values(*cust_fields)[0])
len(b.values(*cust_fields)[0])






No comments:

Post a Comment