修改django 默认查询方式
1、django默认查询方式为get() 返回多个对象时会报错 将其修改为filter()
2、找到路径 site-packages\django\contrib\admin\options.py", line 763, in get_object
3、将get_object中的
return queryset.get(**{field.name: object_id})修改为
return queryset.filter(**{field.name: object_id}).first()
可解决报错的问题
def get_object(self, request, object_id, from_field=None):
"""
Return an instance matching the field and value provided, the primary
key is used if no field is provided. Return ``None`` if no match is
found or the object_id fails validation.
"""
queryset = self.get_queryset(request)
model = queryset.model
field = model._meta.pk if from_field is None else model._meta.get_field(from_field)
try:
object_id = field.to_python(object_id)
return queryset.filter(**{field.name: object_id}).first()
# return queryset.get(**{field.name: object_id})
except (model.DoesNotExist, ValidationError, ValueError):
return None