ChatGPT解决这个技术问题 Extra ChatGPT

django : using select_related and get_object_or_404 together

Is there any way of using get_object_or_404 and select_related together or any other way to achieve the result of using these two together(except from putting it in try/except)??


D
Daniel Roseman

Yes.

obj = get_object_or_404(MyModel.objects.select_related(), whatever=whatever)

Can use multiple where close here obj = get_object_or_404(MyModel.objects.select_related(), whatever=whatever, another=another)