zoukankan      html  css  js  c++  java
  • Unable to get repr for <class 'django.db.models.query.QuerySet'>无法读取出mysql中的数据

    D:python_learnmeiduo_projectmeiduo_mallmeiduo_mallappsgoodsviews.py:38: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'goods.models.SKU'> QuerySet.
      paginator = Paginator(skus, 5)
    ERROR exception 132 Internal Server Error: /list/115/1/
    Traceback (most recent call last):
      File "D:python_learnmeiduo_projectenvlibsite-packagesdjangocorehandlersexception.py", line 41, in inner
        response = get_response(request)
      File "D:python_learnmeiduo_projectenvlibsite-packagesdjangocorehandlersase.py", line 187, in _get_response
        response = self.process_exception_by_middleware(e, request)
      File "D:python_learnmeiduo_projectenvlibsite-packagesdjangocorehandlersase.py", line 185, in _get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
      File "D:python_learnmeiduo_projectenvlibsite-packagesdjangoviewsgenericase.py", line 68, in view
        return self.dispatch(request, *args, **kwargs)
      File "D:python_learnmeiduo_projectenvlibsite-packagesdjangoviewsgenericase.py", line 88, in dispatch
        return handler(request, *args, **kwargs)
      File "D:python_learnmeiduo_projectmeiduo_mallmeiduo_mallappsgoodsviews.py", line 61, in get
        return render(request, 'list.html', context)
      File "D:python_learnmeiduo_projectenvlibsite-packagesdjangoshortcuts.py", line 30, in render
        content = loader.render_to_string(template_name, context, request, using=using)
      File "D:python_learnmeiduo_projectenvlibsite-packagesdjango	emplateloader.py", line 68, in render_to_string
        return template.render(context, request)
      File "D:python_learnmeiduo_projectenvlibsite-packagesdjango	emplateackendsjinja2.py", line 81, in render
        return self.template.render(context)
      File "D:python_learnmeiduo_projectenvlibsite-packagesjinja2environment.py", line 1090, in render
        self.environment.handle_exception()
      File "D:python_learnmeiduo_projectenvlibsite-packagesjinja2environment.py", line 832, in handle_exception
        reraise(*rewrite_traceback_stack(source=source))
      File "D:python_learnmeiduo_projectenvlibsite-packagesjinja2\_compat.py", line 28, in reraise
        raise value.with_traceback(tb)
      File "D:python_learnmeiduo_projectmeiduo_mallmeiduo_mall	emplateslist.html", line 142, in top-level template code
        <a href="detail.html"><img src="{{ sku.default_image_url.url }}"></a>
      File "D:python_learnmeiduo_projectenvlibsite-packagesjinja2environment.py", line 471, in getattr
        return getattr(obj, attribute)
    jinja2.exceptions.UndefinedError: 'goods.models.SKU object' has no attribute 'default_image_url'

    问题:在mysql中查询数据时,代码如下:

    1 skus = category.sku_set.filter(is_launched=True).order_by(sort_field)

    skus 取不到数据,提示 skus: Unable to get repr for <class 'django.db.models.query.QuerySet'>

    解决:

    多方查找,是因为,原模型类中的

    1 default_image_url = models.CharField(max_length=200, default='', null=True, blank=True, verbose_name='默认图片')

    迁移到数据库中变成了default_image,此时如果把模型类中的default_image_url改成default_image,还会报错 'str' object has no attribute 'url'

    最终改成

    1 default_image = models.ImageField(max_length=200, default='', null=True, blank=True, verbose_name='默认图片')

    解决问题!

    总结:

    1 目前所知只有模型类中的外键字段,mysql里会加上 _id,其余字段都是一一对应,不会改变的

    2 ImageField 是用于保存图像文件的字段,有url这个属性,而CharField 没有url 这个属性

    3 网上遇到这个问题的人很多,大部分解决办法有两个方向

      1)因为__str__方法返回的有日期,需要转成str

      2)迁移模型类出错,我遇到的错误属于这个范畴

  • 相关阅读:
    如何将网格式报表打印成其它样式
    拥有与实力不相称的脾气是种灾难——北漂18年(23)
    8.8.1 Optimizing Queries with EXPLAIN
    mysql 没有rowid 怎么实现根据rowid回表呢?
    secondary index
    8.5.5 Bulk Data Loading for InnoDB Tables 批量数据加载
    mysql 中key 指的是索引
    8.5.4 Optimizing InnoDB Redo Logging 优化InnoDB Redo 日志
    8.5.3 Optimizing InnoDB Read-Only Transactions 优化InnoDB 只读事务
    8.5.1 Optimizing Storage Layout for InnoDB Tables InnoDB表的存储布局优化
  • 原文地址:https://www.cnblogs.com/sewen-H/p/13367803.html
Copyright © 2011-2022 走看看