Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django: I am unable to get Django to autocreate an autofield

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading