How to set a field different from id as primary key in django rest model

I am new to django_rest framework and I am having a problem with setting another field as primary key and then use that field as foreign key in another model. These are my models:

class Test(models.Model):
 
     test_id = models.CharField(max_length=50, default="A10", primary_key=True)
     issue_date = models.DateField() 
     total_amount = models.FloatField(max_length=50, default=1.0)


class AnotherModel(models.Model):

     test_identifier = models.ForeignKey(Test, on_delete=models.CASCADE) #The identifier
     refered_date = models.DateField(auto_now_add=True, blank=True)

But when I try to create an AnotherModel in admin page it shows this error:

IntegrityError at /admin/api/anothertest/add/
FOREIGN KEY constraint failed

My question is how can I make the test_id primary key and use it as foreign in second model?

Back to Top