python - Getting an object of a foreign key relationship to show in Django -
the problem occurs image http://i.imgur.com/oexvxvu.png
i vendorprofile name instead of vendorprofile object appears in box. using foreign key relationship purchaseorder in vendorprofile. here code in models.py:
class purchaseorder(models.model): product = models.charfield(max_length=256) vendor = models.foreignkey('vendorprofile') class vendorprofile(models.model): name = models.charfield(max_length=256) address = models.charfield(max_length=512) city = models.charfield(max_length=256)
and here code in admin.py:
class purchaseorderadmin(admin.modeladmin): fields = ['product', 'dollar_amount', 'purchase_date','vendor', 'notes'] list_display = ('product','vendor', 'price', 'purchase_date', 'confirmed', 'get_po_number', 'notes')
so how can display vendorprofile's 'name' in both fields , list_display?
define __unicode__
method vendorprofile
method returning name.
from docs:
the
__unicode__()
method called whenever callunicode()
on object. django usesunicode(obj)
(or related function,str(obj)
) in number of places. notably, display object in django admin site , value inserted template when displays object. thus, should return nice, human-readable representation of model__unicode__()
method.
class vendorprofile(models.model): # fields above def __unicode__(self): return self.name
Comments
Post a Comment