当我将代码从 PHP 移植到 Django/Python 时,我继承了一个设计不佳的数据库。重新设计数据库是对这个项目的一个限制,因此完全不可行。我有两个通过文本字段引用的表
class MemberCompany(models.Model):
id = models.IntegerField(primary_key=True)
company_name = models.TextField(blank=True)
ticker = models.CharField(max_length=45L, blank=True, unique=True)
primary_address = models.TextField(blank=True)
industry_classification = models.TextField(blank=True)
website = models.TextField(blank=True)
industry_id = models.IntegerField(null=True, blank=True)
business_description = models.TextField(blank=True)
dodilio_class = models.TextField(blank=True)
category_id = models.IntegerField(null=True, blank=True)
parent_industry_id = models.IntegerField(null=True, blank=True)
phone = models.CharField(max_length=20L, blank=True)
country_listing = models.CharField(max_length=255L, blank=True)
exchange = models.CharField(max_length=45L, blank=True)
class Meta:
db_table = 'member_companies'
class TeaserTickers(models.Model):
id = models.IntegerField(primary_key=True)
teaser = models.ForeignKey(Idea, related_name='tickers')
######################################################v
# This works to return the ticker text
#ticker = models.CharField(max_length=135L, blank=True)
######################################################v
# This returns: "Unknown column 'teaser_tickers.ticker_id' in 'field list'"
#ticker = models.ForeignKey(MemberCompany, to_field='ticker')
######################################################v
# This returns: "Unknown column 'teaser_tickers.ticker_id' in 'field list'"
ticker = models.ForeignKey(MemberCompany, to_field='ticker')
date_added = models.CharField(max_length=135L, blank=True)
class Meta:
db_table = 'teaser_tickers'
如果数据库不遵循 django 命名约定,如何在两个表/模型类之间形成外键关系。
我尝试定义我自己的关系,但这也有问题(由于某种原因,它没有查询 MemberCompany,而是试图将代码值(例如'AAPL')推送到 MemberCompany 堆栈上。但这里是代码:
class ImproperlyNamedForeignKey(djangoRelated.ForeignKey):
def get_attname(self):
return self.name