1👍
The best way is to have symbol
field as the foreign key to Sttlmnt_typ_Master
.
But if you sure want models like this then add this method to you ModelAdmin
:
class AuctionAdmin(admin.ModelAdmin):
list_display = ('dates', 'sr_no', 'symbol_display', 'series', 'qty')
def symbol_display(self, obj):
symbol = Sttlmnt_typ_Master.objects.filter(symbol=obj.symbol).first()
return symbol.symbol_name if symbol else obj.symbol
symbol_display.short_description = 'Symbol'
This will work but filtering Sttlmnt_typ_Master
for every row in admin is very inefficient. You should consider to switch to “foreign key” option.
EDIT: Foreign key option.
Add the __unicode__
method to Sttlmnt_typ_Master
model and change Auction.symbol
field to ForeignKey
:
class Sttlmnt_typ_Master(models.Model):
symbol = models.CharField(max_length=10, editable=False)
symbol_name = models.CharField(max_length=35, editable=False)
def __unicode__(self):
return self.symbol_name
class Auction(models.Model):
dates = models.DateTimeField(editable=False, null=True)
sr_no = models.IntegerField(editable=False, null=True)
symbol = models.ForeignKey(Sttlmnt_typ_Master, editable=False)
series = models.CharField(max_length=35, editable=False, null=True)
qty = models.IntegerField(editable=False, null=True)
Now changelist in admin will display symbol
field with symbol_name
property. Django admin is smart enough to add select_related('symbol')
to queryset so there will be no additional queries to database.
Of course you should change your import function to populate Auction.symbol
fields with valid Sttlmnt_typ_Master
instances instead of plain text.
EDIT2: Populating Auction.symbol
field.
symbol = 'ZYDUSWELL'
auction.symbol, created = Sttlmnt_typ_Master.objects.get_or_create(
symbol=symbol, defaults={'symbol_name': symbol})