Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#27242 closed New feature (duplicate)

Add get_object_or_none to django.shortcuts

Reported by: Alexey Rogachev Owned by: nobody
Component: Core (Other) Version: master
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 similar to get_object_or_404:

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 (4)

comment:1 Changed 2 years ago by Alexey Rogachev

Description: modified (diff)

comment:2 Changed 2 years ago by Alexey Rogachev

Description: modified (diff)

comment:3 Changed 2 years ago by Tim Graham

Resolution: duplicate
Status: newclosed

Duplicate of #2659, #11352.

comment:4 Changed 2 years ago by Alexey Rogachev

Sorry. I tried to use search to find if similar issues are already exist (I was pretty sure about that), but din't find them.

Now it's clear to me - it needs to be included in own codebase.

Note: See TracTickets for help on using tickets.
Back to Top