TAGS :Viewed: 9 - Published at: a few seconds ago

[ How to create instance of Django Field with Primary Key=True ]

Im working on a django application and I can't seem to get this one piece of code working. I know of some hacky ways to accomplish my task, but it seems like there should be a better way. I have a model class with one field in it:

class MobileUser(models.Model):
    phone_id = models.AutoField(primary_key=True)   

and everything is groovy. However, when I want to create an instance of the class and save it to the database like so:

mobile = MobileUser()

I get a warning saying that MobileUser has no default value arguments, which makes sense. I know I can do things like get the last inserted row/the max id + 1 and pass that in as an argument but I'd prefer not to do that. Any ideas? Oh, and side note, the operation works it just throws a warning.

Answer 1

Why not let django handle it then, just do this:

class MobileUser(models.Model):

Whenever you create an entry it would auto increment the id field.