使用model.objects.filter获取数据库中的数据
2019-07-30 本文已影响0人
TTTRX
之前想要获取数据库中用户名为fgx的项,代码如下:
def saveOfficialPriceToSql(request):
userName="fgx"
shoes=Shoes.objects.filter(user=username)
print(type(shoes))
for shoe in shoes.objects.all():
id=shoe.ShoesId
得到这样的报错信息:
AttributeError: 'QuerySet' object has no attribute objects
这是因为Shoes.objects.filter(user=username)得到的是多项,shoes是一个单项object的一个集合。我们可以把shoes当成一个List,List中的每一项才是我们真正要拿到的内容。因此我们将代码这样修改:
代码如下:
def saveOfficialPriceToSql(request):
userName="fgx"
shoes=Shoes.objects.filter(user=username)
print(type(shoes))
for shoe in shoes:
id=shoe.ShoesId
参考链接
AttributeError: 'QuerySet' object has no attribute
![](https://img.haomeiwen.com/i10387587/e87434d1572a79ea.png)