我想知道是否存在在Django中命名外键字段的约定,该约定映射到to_field
的另一个字段。例如,Django字段名称是模型名称,数据库字段名称是列名还是令人困惑?
class Stations(models.Model):
name = models.CharField()
observations = models.ForeignKey(Observations,
to_field='zone_code'
db_column='zone_code')
class Observations(models.Model):
zone_code = models.IntegerField(unique=True)
metric_a = models.IntegerField()
metric_a = models.IntegerField()
拥有它真的没有意义:
class Stations(models.Model):
name = models.CharField()
observations = models.ForeignKey(Observations,
to_field='zone_code')
class Observations(models.Model):
zone_code = models.IntegerField(unique=True)
metric_a = models.IntegerField()
metric_a = models.IntegerField()
也不是这个
class Stations(models.Model):
name = models.CharField()
zone_code = models.ForeignKey(Observations,
to_field='zone_code')
class Observations(models.Model):
zone_code = models.IntegerField(unique=True)
metric_a = models.IntegerField()
metric_a = models.IntegerField()
因为当数据库中的列实际上是zone_code时,您最终会得到observations_id
或zone_code_id
。以前有人有这个问题吗?