使用model.objects.filter获取数据库中的数据

之前想要获取数据库中用户名为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

支付宝红包码,你领红包我赚赏金;土豪请任意收钱码打赏
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容