Home > OS >  How to change the datatype of Django model field
How to change the datatype of Django model field

Time:12-02

I have a set of django models as follows

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

       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?

I am trying to implement a chained dropdown in my form. I am following SimpleBetterThanComplex tutorial for this

Edit 1: contents of the migration file

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. Could this be affecting the datatypes of the ForeignKeys?

CodePudding user response:

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

When you creates a ForeignKey, Django creates a new field that points to the related model primary key (an automatic id in your case). You can specify another primary key with the primary_key=True argument in the field.

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.

  • Related