sudeep Krishnan
sudeep Krishnan

Reputation: 678

Django foreign key in model

I am doing a project so created one model and imported all data to django admin so my table looks like this

Dates          Sr no   Symbol      Series    Qty
Oct. 19, 2013   245    ZYDUSWELL    EQ      5
Oct. 19, 2013,  244    ZEEL         EQ      96
Oct. 19, 2013,  243    YESBANK      EQ      74
Oct. 19, 2013,  242    WIPRO        EQ      65

I have created another one table which describe the full form of column symbol but the client need to display the full form instead of the displayed symbol in the above table how can I achieve this this my code

The model for given table

class Auction(models.Model):
    dates = models.DateTimeField(editable=False,null=True)
    sr_no = models.IntegerField(editable=False,null=True)
    symbol = models.CharField(max_length=35, editable=False)
    series = models.CharField(max_length=35, editable=False,null=True)
    qty = models.IntegerField(editable=False,null=True)

The model contains full form of symbol

class Sttlmnt_typ_Master(models.Model): 
    symbol = models.CharField(max_length=10, editable=False)
    symbol_name =  models.CharField(max_length=35, editable=False)

-----kindly please help me.......!!!!

Upvotes: 0

Views: 529

Answers (1)

catavaran
catavaran

Reputation: 45555

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})

See the documentation.

Upvotes: 1

Related Questions