﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26638	Allow callable in defaults argument for QuerySet.get_or_create	Maxime Lorant	nobody	"It would be nice if `get_or_create` handles callable in the `defaults` parameter so the defaults value are not computed at each selection. Indeed, if your default values for insertion contains selection of related object or any long computation, it is done at each call, even if the object can be retrieved without insertion (and so the defaults parameter is not used).

In term of code, I would like to do something like this:

{{{
SomeObject.objects.get_or_create(
       key1=value1, 
       key2=value2,
       defaults={
             'key3': lambda: long_processing_to_get_default(),
             # or even
             'key3': lambda: OtherModel.objects.get(pk=request.some_value)
       }
)
}}}


For backward-compatibility and ease of writing, it should still accept values that are not callables. See the patch attached (unit tests are missing so it is not complete!) "	New feature	new	Database layer (models, ORM)	dev	Normal				Unreviewed	0	0	0	0	1	0
