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?