Opened 8 years ago

Last modified 8 years ago

#27242 closed New feature

Add get_object_or_none to django.shortcuts — at Version 1

Reported by: Alexey Rogachev Owned by: nobody
Component: Core (Other) Version: dev
Severity: Normal Keywords: object, model, shortcut
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Alexey Rogachev)

Quite often we need to find object of certain model and return None if it does not exist. I suggest to add this to django.shortcuts:

def get_object_or_none(klass, *args, **kwargs):
    queryset = _get_queryset(klass)
    try:
        return queryset.get(*args, **kwargs)
    except queryset.model.DoesNotExist:
        return None

Yes, we can easily create this in our own code, but maybe consider to add this to the core?

Change History (1)

comment:1 by Alexey Rogachev, 8 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top