Home > Blockchain >  Django: I am unable to get Django to autocreate an autofield
Django: I am unable to get Django to autocreate an autofield

Time:11-30

I am calling BORROWER.objects.create(ssn=Ssn, bname=Name, address=Address, phone=Phone) from views.py to create an entry in my sqlite database. This is my models.py file with the relevant function.

class BORROWER(models.Model):
card_id = models.AutoField(primary_key=True, max_length=7)
ssn = models.CharField(max_length=11)
bname = models.CharField(max_length=71)
address = models.CharField(max_length=79)
phone = models.CharField(max_length=15)
def __str__(self):
    return str(self.card_id)

The database entry is successfully created. However, since I am not specifying a value for the card_id field, the value stays as (<django.db.models.fields.AutoField>,) instead of an actual key. When I try to specify a value for card_id it says something about unexpected argument. Am I calling the create function incorrectly?

Edit: The comma is not there in the original code, but I will try with the suggested edits

CodePudding user response:

Remove comma at the end of the card_id and don't use max_length with AutoFiled ..

class BORROWER(models.Model):
card_id = models.AutoField(primary_key=True)
ssn = models.CharField(max_length=11)
bname = models.CharField(max_length=71)
address = models.CharField(max_length=79)
phone = models.CharField(max_length=15)
def __str__(self):
    return str(self.card_id)
  • Related