Unable to determine ID on entity creation

I have a fairly stub Model:

class Foo(Model):
    id = IntegerField(primary_key=True)
    name = CharField(max_length=255)

In a view:

def create_foo(request):
    m = models.Foo(name="AAA")
    print(m.id) # None
    m.save()
    print(m.id) # None
    return HttpResponse("")

Based on many stackoverflow articles, I’m led to believe that on creating an object, it will not have a primary key until after save() as the PK is assigned by the database. However, in my tests I’ve been unable to programmatically obtain the record ID as part of the save process. While I could wrap re-fetching the record within a transaction, it seems wrong to do that when most documentation suggests what I’m attempting should be possible.

Am I doing something wrong here?

Remove the line defining the id field in your model. Django will create that field for you automatically, and in that case will it be filled in by the database on save.

An IntegerField does not automatically generate a value.

In addition to the recommended solution above, you can also use an AutoField (or BigAutoField) for defining your primary key. (Also see Automatic primary key fields)