[Django]-Use get_object_or_404() method with mongoengine

4👍

It’s time to roll your own shortcut then?

def get_obj_or_404(klass, *args, **kwargs):
    try:
        return klass.objects.get(*args, **kwargs)
    except klass.DoesNotExist:
        raise Http404

def detail(request, poll_id):
    poll = get_obj_or_404(Poll, pk=poll_id)
    return render(request, 'polls/detail.html', {'poll': poll})

I haven’t tested it yet but that’s the basic idea.

I don’t think you are doing anything wrong, it’s just that that Django shortcut doesn’t support the Document class. Check the Django source code, specifically the function get_object_or_404() (which uses the function _get_queryset(), the one that raised the exception you got) then I think you will understand.

Leave a comment