On Wed, Mar 26, 2014 at 4:26 PM, Anssi Kääriäinen <[email protected]>wrote:
> 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 > > Isn't this already possible by defining a custom queryset? And even easier in 1.7 with the introduction of .as_manager()? The only thing the custom Queryset approach doesn't allow is to monkey patch the get_or_none method into *all* managers by default - but frankly, I see that as a good thing. 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 [email protected]. To post to this group, send email to [email protected]. 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/CAJxq849VqOGYmeqaVLnV78WpfS157N23bvE1hVHBUod6GMuGDQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
