Changing django default pk from AutoField to BigAutoField

My model has pk by default with AutoField (integer), but later I found that I need to use BigAutoField instead! And also I have data then with other models referencing the student model :: how do I change the pk field to BigAutoField and also reflects on other link models

class Student(models.Model):
    matric_no = models.CharField(max_length=20,  unique=True)  # first set it to U(random)
    password = models.CharField(max_length=128)
    session = models.ForeignKey(Session, null=True)
    programme = models.ForeignKey(Programme, null=True)
    school = models.ForeignKey(School, null=True)
    course_comb = models.ForeignKey(CourseComb, null=True)
    serial = models.IntegerField(null=True)
    current_level = models.CharField(max_length=3, choices=LEVEL_CHOICES, default='100', null=True)
    last_login = models.DateField(null=True)
    is_active = models.CharField(max_length=1, default=1, choices=((1, 1), (0, 0)))
    created_at = models.DateTimeField(default=timezone.now)
    updated_at = models.DateTimeField(auto_now=True)

      

model referencing Student

class Profile(models.Model):

    student = models.OneToOneField(Student, on_delete=models.CASCADE)
    attachment = models.ImageField(null=True, blank=True, verbose_name="Profile Image")
    surname = models.CharField(max_length=255, null=True, verbose_name="Surname")
    othernames = models.CharField(max_length=255, null=True, verbose_name="Othernames")
    SEX_CHOICES = (
      ("M", "Male"),
      ("F", "Female")
    )

      

+3


source to share


1 answer


Set primary_key=True

in the field definition:

id = models.BigAutoField(primary_key=True)

      

If you want to use this across multiple models, you can also create an abstract model and let others inherit from it:



class BigPkAbastract(models.Model):
    id = models.BigAutoField(primary_key=True)

    class Meta:
        abstract = True

      

And in other models:

class SomeModel(BigPkAbastract):
    <your model here>

      

+3


source







All Articles