想要从基本模型访问我的子类名称:
class Asset(models.Model):
name = models.CharField(max_length=50, blank=False)
country = models.ForeignKey(Country)
industry = models.ForeignKey(Industry)
ric = models.CharField(max_length=50, blank=False)
notes = models.TextField(blank=True)
def __unicode__(self):
return "%s - %s" % (self.name, self.__class__.__name__)
class Equity(Asset):
ticker = models.CharField(max_length=6, blank=False)
start_of_day = models.TimeField(default=None, null=True, blank=True, help_text="Start time to query TR")
query_frequency = models.IntegerField(help_text="In seconds, timeinterval between checks (intraday))")
end_of_day = models.TimeField(default=None, null=True, blank=True, help_text='End time to query TR')
class Meta:
verbose_name_plural = "Equities"
class CashManagementInstrument(Asset):
currency = models.ForeignKey(Currency)
rate = models.DecimalField(max_digits=12, decimal_places=4)
maturity_date = models.DateTimeField(default=datetime.now, blank=False)
class Meta:
verbose_name_plural = "Mutual Funds"
在我的管理员中,我有一个与资产相关的模型。就在那里,我想得到它的名字和它的子类,ej:
Apple - Equity
持有 - CashManagementInstrument
由于
答案 0 :(得分:0)
最后我以这种方式解决了我的问题,这不是最优雅的方式,但它有效:)
class Asset(models.Model):
name = models.CharField(max_length=50, blank=False)
country = models.ForeignKey(Country)
industry = models.ForeignKey(Industry)
ric = models.CharField(max_length=50, blank=False)
notes = models.TextField(blank=True)
def __unicode__(self):
return "%s - %s" % (self.get_child_asset(), self.name)
def get_child_asset(self):
if hasattr(self, 'equity'):
return "Equity"
elif hasattr(self, 'realstate'):
return "Real State"
elif hasattr(self, 'fixedincome'):
return "Fixed Income"
elif hasattr(self, 'mutualfunds'):
return "Mutual Funds"
elif hasattr(self, 'cashmanagementinstrument'):
return "CMI"