之间有什么区别
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/