且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django:get()返回了多个项目-返回了3

更新时间:2023-11-22 23:50:52

只需阅读django文档 https://docs.djangoproject.com/en/2.0/topics/http/shortcuts/#get-object-or-404

just go through the django documentation https://docs.djangoproject.com/en/2.0/topics/http/shortcuts/#get-object-or-404

它内部调用get() https://docs.djangoproject.com/zh-CN/2.0/ref/models/querysets/#django.db.models.query.QuerySet.get

it internally calls get() https://docs.djangoproject.com/en/2.0/ref/models/querysets/#django.db.models.query.QuerySet.get

如果找到多个对象,它将引发MultipleObjectsReturned.

it will raise MultipleObjectsReturned if more than one object was found.

您需要将primary_key或任何其他fild传递给get_object_or_404,例如:get_object_or_404(MyModel,pk = 1)

you need to either pass primary_key or any other fild to get_object_or_404 like:get_object_or_404(MyModel, pk=1)

,或者如果您想要多个记录,则可以使用filter().

or you can use filter() if you want multiple records.