سؤال

As the title suggest, and if it doesn't raise 404, do I have any method to tell it to?

هل كانت مفيدة؟

المحلول

https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#get-object-or-404

Note: As with get(), a MultipleObjectsReturned exception will be raised if more than one object is found.

edit:

def my_view(request):
    try:
        my_object = get_object_or_404(MyModel, something=1)
    except MyModel.MultipleObjectsReturned:
        # Maybe do something.
        raise Http404
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top