用于命名使用to_field参数的ForeignKey的Django约定?

时间:2017-05-05 19:56:07

标签: python django foreign-keys relational-database

我想知道是否存在在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_idzone_code_id。以前有人有这个问题吗?

0 个答案:

没有答案