Django models Django:覆盖ManyToManyField中的默认字段名

Django models Django:覆盖ManyToManyField中的默认字段名,django-models,Django Models,你好 在下面的示例中使用ManyToManyField会导致django自动创建一个名为country_region的模型,其中包含字段country_id和region_id,这非常酷。但是,如果region\u country数据库表(由于超出此线程范围的原因)的字段名不是country\u id和region\u id,该怎么办 Django根据相关表的模型名在多对多表中生成字段名,因此我发现实现这一点的唯一方法是更改两个模型名。但这迫使我有我不想要的模型名!如何告诉django在自动多对

你好

在下面的示例中使用ManyToManyField会导致django自动创建一个名为country_region的模型,其中包含字段country_id和region_id,这非常酷。但是,如果region\u country数据库表(由于超出此线程范围的原因)的字段名不是country\u id和region\u id,该怎么办

Django根据相关表的模型名在多对多表中生成字段名,因此我发现实现这一点的唯一方法是更改两个模型名。但这迫使我有我不想要的模型名!如何告诉django在自动多对多模型中使用哪些字段名

我已经尝试使用ManyToManyField的through=关键字参数显式指定多对多模型,这是可行的,但是基于区域模型的表单不允许保存

非常感谢您的建议

谢谢, 兰德尔


不确定,但您是否尝试过
verbose\u name
属性?这样:
region=models.ManyToManyField(Country,db\u table='region\u Country',verbos\u name='override')
好主意,刚刚尝试过,没有任何影响。。。
class Country(models.Model):
    country_id = models.AutoField(primary_key=True)
    country_name = models.CharField(max_length=200)

    class Meta:
        managed = False
        db_table = 'country'

    def __unicode__(self):
        return '%s' % (self.country_name)

class Region(models.Model):
    region_id = models.AutoField(primary_key=True)
    region_name = models.CharField(max_length=200)
    region = models.ManyToManyField(Country, db_table='region_country')

    class Meta:
        managed = False
        db_table = 'region'