0

我在 django 中创建模型。这是我在模型文件中的配置。我想创建一个 governmentoffical引用县和公民的表。但是,当我运行 manage.py sqlal 时,它更改了我在政府官方表格中的列名。

class counties(models.Model):
    name = models.CharField(max_length=50, primary_key=True, unique=True)
    population = models.IntegerField()
    governer = models.CharField(max_length=75)

class citizen(models.Model):
    ssn = models.CharField(max_length=40, primary_key=True)
    citizen_name = models.CharField(max_length=50, unique=True)
    age = models.IntegerField()
    income = models.IntegerField()
    username = models.CharField(max_length=30)
    password = models.CharField(max_length=30)

class governmentOfficial(models.Model):
    countyName = models.ForeignKey(counties, primary_key=True)
    offical_name = models.ForeignKey(citizen, to_field='citizen_name')
    position = models.CharField(max_length=50)
    year_elected = models.IntegerField(max_length=4)
    party = models.CharField(max_length=25)
    county_username=models.CharField(max_length=20)
    county_password=models.CharField(max_length=20)

这是我的 sql 输出。

CREATE TABLE `troywebsite_governmentofficial` (
    `countyName_id` varchar(50) NOT NULL PRIMARY KEY,
    `offical_name_id` varchar(50) NOT NULL,
    `position` varchar(50) NOT NULL,
    `year_elected` integer NOT NULL,
    `party` varchar(25) NOT NULL,
    `county_username` varchar(20) NOT NULL,
    `county_password` varchar(20) NOT NULL
)
;
ALTER TABLE `troywebsite_governmentofficial` ADD CONSTRAINT `countyName_id_refs_name_2c9a2c94` FOREIGN KEY (`countyName_id`) REFERENCES `troywebsite_counties` (`name`);
ALTER TABLE `troywebsite_governmentofficial` ADD CONSTRAINT `offical_name_id_refs_citizen_name_6f795c2a` FOREIGN KEY (`offical_name_id`) REFERENCES `troywebsite_citizen` (`citizen_name`);

CREATE INDEX `troywebsite_governmentofficial_effdd6a5` ON `troywebsite_governmentofficial` (`offical_name_id`);
CREATE INDEX `troywebsite_solider_c5b4e228` ON `troywebsite_solider` (`solider_name_id`);

有没有办法把里面的官员拿出来,CREATE INDEX留下来当专栏?countyNameOfficial_name

4

1 回答 1

1

您可以使用 db_column 参数来调用任何您喜欢的列。

https://docs.djangoproject.com/en/dev/ref/models/fields/#db-column

CountyName = models.ForeignKey(counties, primary_key=True, db_column='whatever')

于 2013-06-03T21:31:03.593 回答