30👍
✅
get_or_create()
simply returns a tuple of the two values. You can then use sequence unpacking to bind the two tuple entries to two names, like in the documentation example:
p, created = Person.objects.get_or_create(
first_name='John', last_name='Lennon',
defaults={'birthday': date(1940, 10, 9)})
6👍
It returns a tuple. It sounds like you knew that functions could do this, just not that you could assign the results directly to two variables!
See the Django documentation for get_or_create
:
# Returns a tuple of (object, created), where object is the retrieved
# or created object and created is a boolean specifying whether a new
# object was created.
obj, created = Person.objects.get_or_create(first_name='John', last_name='Lennon',
defaults={'birthday': date(1940, 10, 9)})
- [Django]-Sending post data from angularjs to django as JSON and not as raw content
- [Django]-How to get getting base_url in django template
- [Django]-Django Admin app or roll my own?
4👍
Using tuples/tuple unpacking is often considered as a quite “pythonic” way of returning more than one value.
- [Django]-Django Rest Framework model serializer with out unique together validation
- [Django]-Remove pk field from django serialized objects
- [Django]-Django-nonrel + Django-registration problem: unexpected keyword argument 'uidb36' when resetting password
Source:stackexchange.com