Django: Return foreign key's custom field value substitute for id number in filter() -


i have below models.py

class site(models.model):     site_name = models.charfield(max_length=10)     site_description = models.charfield(max_length=30, blank=true, null=true)     def __unicode__(self):         return self.site_name  class asset(models.model):     stat_choices = (('available', 'available'), ('deployed', 'deployed'), ('down', 'down'))     asset_id = models.charfield(max_length=30, primary_key=true)     owner = models.charfield(max_length=30, blank=true, null=true)     status = models.charfield(choices=stat_choices, max_length=30, blank=true, null=true)     site = models.foreignkey(it_asset_site)     notes = models.charfield(max_length=300, blank=true, null=true)      def __unicode__(self):         return self.asset_id 

as known, table “site” create site_id pk default. after imported data table "asset", in django's admin, "site" display "site_name". effect come "def unicode(self):". want same effect

f = asset.objects.filter(site__site_name__contains='nng1') 

but in "f", "site" field display number(site_id) not nng1(site_name), sample:

b = f.filter(site__site_name__contains='nng1') in [44]: b.values()[0] out[44]:  {'asset_id': u'3914100271',  'owner': u'',  'status': none,  'site_id': 8l,*****<-now*****  'notes': u'', } 

had way make filter result become:

{'asset_id': u'3914100271',  'owner': u'',  'status': none,  'site_name': u'nng1',*****<-hope*****  'notes': u'', } 

like django admin page show? thanks

you can specify fields parameters in values():

b.values('asset_id', 'owner', 'status', 'site__name', 'notes')[0] 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -