Liondancer
Liondancer

Reputation: 16469

basic python with Django

I'm currently reading some of the Django docs and I've come across this

    obj, created = Person.objects.get_or_create(first_name='John', last_name='Lennon',
              defaults={'birthday': date(1940, 10, 9)})

I'm still learning python in the process. I'm a bit confused with the "obj, created = " part of the code. I assume that both "obj" and "created" perform

    Person.objects.get_or_create(first_name='John', last_name='Lennon',
              defaults={'birthday': date(1940, 10, 9)})

Upvotes: 0

Views: 56

Answers (1)

Paul Tomblin
Paul Tomblin

Reputation: 182782

Person.objects.get_or_create returns two values, which are assigned into obj and created respectively. obj is the actual object that was gotten or created, and created is a flag that tells you if it was created (or gotten).

Upvotes: 2

Related Questions