[英]How to change the datatype of Django model field
I have a set of django models as follows我有一套django型号如下
class FirstCategory(models.Model):
first_category = models.CharField(max_length=200)
def __str__(self):
return self.first_category
class SecondCategory(models.Model):
first_category = models.ForeignKey(FirstCategory, on_delete=models.CASCADE)
second_category = models.CharField(max_length=200)
def __str__(self):
return self.second_category
class ThirdCategory(models.Model):
first_category = models.ForeignKey(FirstCategory, on_delete=models.CASCADE)
second_category = models.ForeignKey(SecondCategory, on_delete=models.CASCADE)
third_category = models.CharField(max_length=200)
def __str__(self):
return self.third_category
class FinalList(models.Model):
unique_number = models.BigIntegerField()
name = models.CharField(max_length=200)
first_category = models.ForeignKey(FirstCategory, on_delete=models.SET_NULL, null=True, db_column="first_category")
second_category = models.ForeignKey(SecondCategory, on_delete=models.SET_NULL, null=True, db_column="second_category")
third_category = models.ForeignKey(ThirdCategory, on_delete=models.SET_NULL, null=True, db_column="third_category")
def __str__(self):
return self.name
When I migrate the models and check the datatypes of the fields in the FinalList
table I get the following当我迁移模型并检查
FinalList
表中字段的数据类型时,我得到以下信息
column_name | data_type
---------------------------+-------------------
third_category | bigint
first_category | bigint
second_category | bigint
unique_number | bigint
name | character varying
I had specified the first, second, and third categories as Character fields.我已将第一、第二和第三类指定为字符字段。 Why is it being migrated as BigInt?
为什么要迁移为 BigInt?
I am trying to implement a chained dropdown in my form.我正在尝试在我的表单中实现一个链式下拉菜单。 I am following SimpleBetterThanComplex tutorial for this
我正在关注SimpleBetterThanComplex教程
In the migration file, I have the following在迁移文件中,我有以下内容
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
I am not specifying this myself.我自己没有具体说明。 I think Django creates this as a auto primary field.
我认为 Django 将其创建为自动主字段。 Could this be affecting the datatypes of the
ForeignKeys
?这会影响
ForeignKeys
的数据类型吗?
Django itself creates a primary key on each model, automatically named id
: https://docs.djangoproject.com/en/3.2/topics/db/models/#automatic-primary-key-fields Django 本身在每个 model 上创建一个主键,自动命名为
id
: https://docs.djangofield.com/en/3.2/topics-keysmodels/automatic/primary-keys#
When you creates a ForeignKey
, Django creates a new field that points to the related model primary key (an automatic id in your case).当您创建
ForeignKey
时, Django 会创建一个新字段,该字段指向相关的 model 主键(在您的情况下是自动 id)。 You can specify another primary key with the primary_key=True
argument in the field.您可以在字段中使用
primary_key=True
参数指定另一个主键。
class FirstCategory(models.Model):
first_category = models.CharField(max_length=200, primary_key=True)
def __str__(self):
return self.first_category
Additionally, Django provides the pk
shortcut property to access the primary key in a model, regardless of whether it is automatic or manually specified.此外,Django 提供
pk
快捷方式属性来访问 model 中的主键,无论它是自动指定的还是手动指定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.