user1076881
user1076881

Reputation: 251

Django model object initialization

If I do

obj = Object()
obj.att1 = 'test'
obj.att2 = 'test'
obj.save()

obj.id --> works fine

But if I do

obj=Object(att1='test',att2='test').save()

Doing obj.id --> obj seems to be Nonetype at this stage

Is this the case?

Upvotes: 3

Views: 5816

Answers (1)

David H. Clements
David H. Clements

Reputation: 3638

I don't know the exact framework you are using, but I am going to take a guess as to the problem:

Object(att1='test',att2='test').save()

The save() function doesn't appear to return the Object instance, it returns None. So you would normally:

obj=Object(att1='test',att2='test')
obj.save()

Then check obj.id.

Upvotes: 8

Related Questions