gpt4 book ai didi

django - Django 的 filter() 和 get() 方法的区别

转载 作者:行者123 更新时间:2023-12-03 10:47:38 25 4
gpt4 key购买 nike

之间有什么区别

mymodel=model.objects.get(name='pol')


mymodel=model.objects.filter(name='pol')

最佳答案

Django QuerySet docs对此非常清楚:
get(**kwargs)¶

Returns the object matching the given lookup parameters, which should be in the format described in Field lookups.

get() raises MultipleObjectsReturned if more than one object was found. The MultipleObjectsReturned exception is an attribute of the model class.

get() raises a DoesNotExist exception if an object wasn't found for the given parameters. This exception is also an attribute of the model class.


filter(**kwargs)

Returns a new QuerySet containing objects that match the given lookup parameters.



基本使用 get()当你想得到 一个唯一的对象 , 和 filter()当你想得到 所有对象 匹配您的查找参数。

关于django - Django 的 filter() 和 get() 方法的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3221938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com