Hello Sometimes when adding tags to an existing object, I am getting the following error. get() returned more than one Tag -- it returned 2! Here is the code I am using ... publication.tags.set(all_tags, clear=True) publication.save() # ...
return clone._result_cache[0] if not num: raise self.model.DoesNotExist( "%s matching query does not exist." % self.model._meta.object_name ) raise self.model.MultipleObjectsReturned( "get() returned more than one %s -- it returned %s!" % (self.model._meta.object_name, num) ) . ...
1.django的get方法是从数据库的取得一个匹配的结果,返回一个对象,如果记录不存在的话,它会报错。 3.django的filter方法是从数据库的取得匹配的结果,返回一个对象列表,如果记录不存在的话,它会返回[]。 4.如果你用django的get去取得关联表的数据的话,无论关联表有多少记录的都不会报错。
Django的orm中get和filter的不同首先对比下两个函数文档上的解释。get Returnstheobjectmatchingthegivenlookupparameters,whichshouldbeintheformatdescribedinFieldlookups.get()raisesMultipleObjectsReturnedifmorethanoneobjectwasfound.TheMultipleObjectsReturnedexceptionisanattributeofthemodelclass.get()raisesaDoes...
我经常使用的是1.5版本号的django,就以此为例来说说吧。 文档 首先对照下两个函数文档上的解释。 get 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 foun...
首先对比下两个函数文档上的解释。getReturns 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 fo django filter get 转载 ...
if num == 1: return clone._result_cache[0] if not num: raise self.model.DoesNotExist("%s matching query does not exist." % self.model._meta.object_name) raise self.model.MultipleObjectsReturned("get() returned more than one %s -- it returned %s! Lookup parameters were %s" ...
Note: Like with get(), an MultipleObjectsReturned will be raised if more than one object is found. """ queryset = _get_queryset(klass) try: return queryset.get(*args, **kwargs) except queryset.model.DoesNotExist: return None comment:9 by Malcolm Tredinnick, 14年 ago 处理结果: →...
Django provides a way to display variables in a template by using the {{ }} syntax. Any variable placed inside the double curly braces is evaluated for its text content and then placed into the template. If we wanted to display the dog's name, for example, we could use {{dog.name}}...
Now, we should always use theget()carefully as if the QuerySet doesn’t find any object then, it will return aModel.DoesNotExistexception. And if the QuerySet finds more than one object then, it will return theModel.MultipleObjectsReturnedexception. Both the errors are shown in the example...