So basically you want:

https://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create

but without the create part - just an empty(), unsaved, object?

This existing method is so close to what you want - that I'd be inclined to 
explore whether it could be modified to do more.

In forms we already have the .save(commit=False) pattern - so it is not 
without reason that we could adopt that for the notion of 'create' on that 
method.

Because get_or_create takes an open list of kwargs, we can't just use 
'commit' - but we could use '__commit'

So it would look like:

obj, created = A.objects.get_or_create(slug="hello", __commit=False)

what the value of created should be in this case is tricky - arguments 
could be made either way.

so I'm far from certain this sort of overloading of get_or_create is 
workable, but if a method were added, but if  method were to be added, I'd 
argue it should follow the get_or_* naming pattern.

This is so easy to do in a custom manager or queryset, the question is 
whether this is a common enough need to bake into the default.

-Preston


On Tuesday, October 9, 2012 7:05:17 AM UTC-7, Ole Laursen wrote:
>
> Hi!
>
> What do people think of
>
>   A.objects.getdefault(slug="hello")  # returns None if slug doesn't exist
>   A.objects.getdefault(slug="hello", default=A())  # returns empty object 
> if slug doesn't exist
>
> I find that in practice, most of the time it would be better to get None 
> back instead of the DoesNotExist exception, but changing .get() is of 
> course impossible without breaking the API.
>
> I do think that
>
>   A.objects.get(slug="hello", default=None)
>
> is more elegant, but it might break some code. Although it does seem 
> really unlikely anyone would add a field named "default" to their model and 
> then use it to locate a unique object.
>
> The only related info I found in the issue tracker and on this list was a 
> thread from 2006.
>
> Ole
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-developers/-/XsLFfxITG7IJ.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to