3

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?

0

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks dclements. Your method works fine. Im using python language in Django framework by the way.
A Django one-liner that creates, saves, and returns the object is Object.objects.create(att1='test', att2='test').
This is perfect answer. Because what you are doing is just two different things. In the first case you creating the object and obj is OBJECT. The second case - the obj is the result of the FUNCTION.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.