Shai.
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/201403131905.09028.shai%40platonix.com.
For more options, visit https://groups.google.com/d/optout.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAHKQagFCyR2GGcY%2BV%2BGzdR%3DKi3P9%2BTVbT4BzVD_bDoJBN1w6Qw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAKFtT_1UhAciOR-OSYW734ftsCRJ6%3DRTiKbhvNktUQVf%3DSHsQg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/05cbe701-87ec-4e2d-8b62-a5ba4eaf6e16%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/05cbe701-87ec-4e2d-8b62-a5ba4eaf6e16%40googlegroups.com.
On Thursday 13 March 2014 14:34:18 Josh Smeaton wrote:Since 1.6, you should just be using first(). Compared to the
> +1 on get_or_none. It seems to be a pattern that comes up quite a lot in
> user code, and I know I've had use for it lots of times.
try-get-except-DoesNotExist-return-None pattern, it is only missing the
validation that there is, indeed, at most one objet matching the criteria. In
a large majority of the cases I've seen, that validation is nice to have if it
comes for free, but not worth a special effort, because it is taken care of by
database constraints (in a significant part, the criteria just select by pk).
I would be happy to have a validating first(),
but the current proposition, as
far as I understand, just overlaps existing API almost completely. I wouldn't
veto it, but a strong -0 from me.
Shai.
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/1857272.IFIdcI8OYn%40deblack.
Shai has changed my mind. Most uses of the get or none pattern that I've used could be replaced by .first(), since it's unlikely there will be multiple objects with the kind of query you'd be using with a get.
I really dislike the get_or_create shortcut syntax, and I don't think a good name for get_or_none exists for the manager method.
.get(or=None) (of some description) would be my preference, but even that is ugly and confuses the existing API with "special" keywords that aren't actually a filter.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/8c640130-e89b-475b-8049-7562eb195787%40googlegroups.com.
.get(or=None) (of some description) would be my preference, but even that is ugly and confuses the existing API with "special" keywords that aren't actually a filter.I would be strong -1 on having a special keyword.
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/4aa7d1e3-4fb3-429e-a95a-6e52cc9b511a%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/3039581.NUMVpPu4sL%40deblack.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/3623f921-4c6f-4e5f-af9a-fc8e5d656c5b%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAPFOCZdmZaQjw9LaqT1BczNPuuu1OageEjw%3DDfrArDV5RbMpgw%40mail.gmail.com.
I'll give it a couple more days for a BDFL to gives the thumbs up/down.
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/815dc10d-0e4d-4db9-b42a-e2adf9760786%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAJxq84_rYfpfSGicAKFDrLdtTKW9q1tr%2BM03xXx_qRCi4usTEw%40mail.gmail.com.
I'd like to solve this with a way to add methods to QuerySets. This would be useful for those who want get_or_none() or other similar shortcut methods which Django doesn't provide. But more importantly this would let third party apps to provide queryset methods.
If this was possible, then one could do something like this:
@register_qs_method
def get_or_none(self, *args, **kwargs):
try:
return self.get(*args, **kwargs)
except ObjectDoesNotExist:
return None
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/53328F22.7000606%40thl.fi.
On Thu, Mar 20, 2014 at 9:31 PM, Florian Apolloner <f.apo...@gmail.com> wrote:
On Thursday, March 20, 2014 2:01:25 PM UTC+1, Cal Leeming [Simplicity Media Ltd] wrote:I'll give it a couple more days for a BDFL to gives the thumbs up/down.
Well, we don't have BDFLs anymore and Shai already said he is -0 on it, count me in with a relatively strong -0 too. I'd be a bit more open to it if you could a better name. That said I generally agree with Shai about the validation, eg this should be handled by the database constraints already -- otherwise selecting via get doesn't make much sense imo.
Count me as a -0 as well. I simply don't see the problem with catching exceptions. Changing the name doesn't modify my objections.
I'm probably a true neutral 0 on a django.shortcuts method mirroring get_object_or_404 (which, BTW, is what the way-back-in-2007 thread was proposing). I still don't see the point, but at least it's in a shortcuts module, and clearly labelled as such, not a method on a manager duplicating existing functionality.Yours,Russ Magee %-)
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAJxq84_rYfpfSGicAKFDrLdtTKW9q1tr%2BM03xXx_qRCi4usTEw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/1ef75fc6-90c2-4d70-9aa0-d2c1cac25c49%40googlegroups.com.
On Tue, Mar 25, 2014 at 11:40 PM, Russell Keith-Magee <rus...@keith-magee.com> wrote:Then why did first() [1] get added? The only difference is that first() adds a reference to [0], but it still saves the equal amount of code and is still there for the same reasons of convenience, no?
On Thu, Mar 20, 2014 at 9:31 PM, Florian Apolloner <f.apo...@gmail.com> wrote:
On Thursday, March 20, 2014 2:01:25 PM UTC+1, Cal Leeming [Simplicity Media Ltd] wrote:I'll give it a couple more days for a BDFL to gives the thumbs up/down.
Well, we don't have BDFLs anymore and Shai already said he is -0 on it, count me in with a relatively strong -0 too. I'd be a bit more open to it if you could a better name. That said I generally agree with Shai about the validation, eg this should be handled by the database constraints already -- otherwise selecting via get doesn't make much sense imo.
Count me as a -0 as well. I simply don't see the problem with catching exceptions. Changing the name doesn't modify my objections.
In my ideal world, the get(default=None) approach would be what we would do; but, as others have pointed out, default is a valid column name, so this option isn't available to us. We already have a shortcut for get_object_or_404; a matching get_object_or_none makes sense to me, and puts the API where it make sense to me - as a shortcut for someone who is repeating the "catch DoesNotExist" pattern regularly and wants an easier way.
On Wed, Mar 26, 2014 at 8:19 PM, Cal Leeming [Simplicity Media Ltd] <cal.l...@simplicitymedialtd.co.uk> wrote:
On Tue, Mar 25, 2014 at 11:40 PM, Russell Keith-Magee <rus...@keith-magee.com> wrote:Then why did first() [1] get added? The only difference is that first() adds a reference to [0], but it still saves the equal amount of code and is still there for the same reasons of convenience, no?
On Thu, Mar 20, 2014 at 9:31 PM, Florian Apolloner <f.apo...@gmail.com> wrote:
On Thursday, March 20, 2014 2:01:25 PM UTC+1, Cal Leeming [Simplicity Media Ltd] wrote:I'll give it a couple more days for a BDFL to gives the thumbs up/down.
Well, we don't have BDFLs anymore and Shai already said he is -0 on it, count me in with a relatively strong -0 too. I'd be a bit more open to it if you could a better name. That said I generally agree with Shai about the validation, eg this should be handled by the database constraints already -- otherwise selecting via get doesn't make much sense imo.
Count me as a -0 as well. I simply don't see the problem with catching exceptions. Changing the name doesn't modify my objections.first() was primarily added as an analog for latest().Personally, I see a significant difference between first()/latest() and get_or_none().
first()/latest() is a specific, common query that has been optimized: list all, order by X, give me the first one.
get_or_none() is a second version of an existing query: get(), but with a different return value. To me, this is duplication of an API, not a different query.
In my ideal world, the get(default=None) approach would be what we would do; but, as others have pointed out, default is a valid column name, so this option isn't available to us.
We already have a shortcut for get_object_or_404; a matching get_object_or_none makes sense to me, and puts the API where it make sense to me - as a shortcut for someone who is repeating the "catch DoesNotExist" pattern regularly and wants an easier way.
Yours,Russ Magee %-)--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAJxq84_281FRq4eJ6mxjBfWREi_50naTg%2B6mcvLBKSozeQwjYw%40mail.gmail.com.
Hello,
I'm not sure this thread is going anywhere and I don't care either way.
If you've been reading up to this point...
... either you have too much spare time. Rather than rehashing this debate,
may I suggest triaging some tickets? That would really help!
https://docs.djangoproject.com/en/dev/internals/contributing/triaging-tickets/
https://code.djangoproject.com/query?status=!closed&stage=Unreviewed&desc=1&order=changetime
... or you really want this feature badly. I'll just leave this here:
from django.db.models.query import QuerySet
from django.db.models.manager import Manager
def queryset_get_or_none(self, *args, **kwargs):
try:
return self.get(*args, **kwargs)
except self.model.DoesNotExist:
pass
QuerySet.get_or_none = queryset_get_or_none
def manager_get_or_none(self, *args, **kwargs):
return self.get_queryset().get_or_none(*args, **kwargs)
Manager.get_or_none = manager_get_or_none
(That kind of monkey-patching isn't the worst because even if
Django grows a method with that name, it will behave the same.)
--
Aymeric.
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/9AE24931-1E65-44C4-808E-AFD232B697C7%40polytechnique.org.
--
Aymeric.
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/F8FA7A7F-2A4C-4745-9CD9-E8D329ED39F8%40polytechnique.org.