'list' object is not callable
来源:6-2 商品类别数据接口-2
慕圣3437809
2017-11-08
在第6-2节商品类别数据接口 编辑好代码后,运行http://127.0.0.1:8000/categorys/就报错,'list' object is not callable,你们有没有遇到过,怎么就解决的??
apps.goods.serialiaer.py代码:
from rest_framework import serializers from goods.models import Goods, GoodsCategory class CategorySerializer(serializers.ModelSerializer): """ 商品类别序列化 """ class Meta: model = GoodsCategory # 将category序列化 fields = "__all__" class GoodsSerializer(serializers.ModelSerializer): category = CategorySerializer() class Meta: model = Goods fields = "__all__" # 调用"__all__",可以将所有字段序列化。
apps.goods.views.py代码:
from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import mixins from rest_framework import generics from rest_framework.pagination import PageNumberPagination from rest_framework import viewsets from django_filters import rest_framework from rest_framework import filters from .filters import GoodsFilter from .models import Goods, GoodsCategory from .serializers import GoodsSerializer, CategorySerializer # Create your views here. class GoodsPagination(PageNumberPagination): page_size = 10 # 每一页显示的信息条数 page_size_query_param = 'page_size' # 指定某一页要多少条信息 page_query_param = "p" # p代表第几页 max_page_size = 100 # 页面最大数量是100条信息 class GoodsListViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): """ 商品列表页、分页、过滤、搜索、排序 """ queryset = Goods.objects.all() serializer_class = GoodsSerializer # 商品列表页 pagination_class = GoodsPagination # 分页 filter_backends = (rest_framework.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter,) filter_class = GoodsFilter # 过滤 search_fields = ('name', 'goods_brief', 'goods_desc') # 搜索 ordering_fields = ('sold_num', 'add_time') # 排序 class CategoryViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): """ list: 商品分类列表数据 """ queryset = GoodsCategory.objects.all() serializer_class = CategorySerializer
MxShop.urls.py的配置:
# from django.contrib import admin from django.conf.urls import url, include import xadmin from MxShop.settings import MEDIA_ROOT from django.views.static import serve from rest_framework.documentation import include_docs_urls from rest_framework.routers import DefaultRouter from goods.views import GoodsListViewSet, CategoryViewSet # 生成router对象 router = DefaultRouter() # 配置goods的url router.register(r'goods', GoodsListViewSet, base_name="goods") # 配置categorys的url router.register(r'categorys', CategoryViewSet, base_name="categorys") goods_list = GoodsListViewSet.as_view({ 'get': 'list', }) urlpatterns = [ url(r'^xadmin/', xadmin.site.urls), url(r"^media/(?P<path>.*)$", serve, {"document_root":MEDIA_ROOT}), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')), url(r'^', include(router.urls)), url(r"docs/$", include_docs_urls(title="美点商店")), ]
错误提示:
TypeError at /categorys/ 'list' object is not callable Request Method:GET Request URL:http://127.0.0.1:8000/categorys/ Django Version:1.11.6 Exception Type:TypeError Exception Value:'list' object is not callable Exception Location:F:\Python\virtualenv\VueShop\lib\site-packages\rest_framework\generics.py in paginator, line 164 Python Executable:F:\Python\virtualenv\VueShop\Scripts\python.exe Python Version:3.6.2 Python Path:['F:\\GitHub\\meidianshop\\MxShop\\extra_apps', 'F:\\GitHub\\meidianshop\\MxShop\\apps', 'F:\\GitHub\\meidianshop\\MxShop', 'F:\\GitHub\\meidianshop\\MxShop', 'F:\\GitHub\\meidianshop\\MxShop', 'F:\\GitHub\\meidianshop\\MxShop\\apps', 'F:\\GitHub\\meidianshop\\MxShop\\extra_apps', 'F:\\Python\\virtualenv\\VueShop\\Scripts\\python36.zip', 'F:\\Python\\virtualenv\\VueShop\\DLLs', 'F:\\Python\\virtualenv\\VueShop\\lib', 'F:\\Python\\virtualenv\\VueShop\\Scripts', 'C:\\Anaconda3\\Lib', 'C:\\Anaconda3\\DLLs', 'F:\\Python\\virtualenv\\VueShop', 'F:\\Python\\virtualenv\\VueShop\\lib\\site-packages'] Server time:星期三, 8 十一月 2017 00:12:21 +0800
写回答
1回答
-
慕圣3437809
提问者
2017-11-08
在商品分类列表里面加上分页就可以了。。
class CategoryViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): """ list: 商品分类列表数据 """ queryset = GoodsCategory.objects.filter(category_type=1) serializer_class = CategorySerializer pagination_class = GoodsPagination # 分页
可是课堂上老师讲的没有加分页。。。
如果不加分页,该怎么解决这个错误呢??
我试着不继承mixins.ListModelMixin,运行时就报错404,没有这个url。。
求各位想想办法吧
042017-11-10
Python前后端分离开发Vue+Django REST framework实战
Django REST framework课程视频,RESTFul API前后端分离开发
2873 学习 · 2457 问题
相似问题