这是urls.py
from django.conf.urls import url, include
from django.contrib import admin
from api import views
from rest_framework.urlpatterns import format_suffix_patterns
from rest_framework import routers
router = routers.DefaultRouter()
router.register('catalogs',views.CatalogView,'catalog')
router.register('companies',views.CompanyView)
router.register('users',views.UserView)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include(router.urls)),
]
以下是错误:
Unhandled exception in thread started by <function wrapper at 0x7ff9fc5b6848>
Traceback (most recent call last):
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 124, in inner_run
self.check(display_num_errors=True)
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
return check_method()
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 256, in check
for pattern in self.url_patterns:
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 407, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 400, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/wishbook/Desktop/Dev/rest/settings/urls.py", line 16, in <module>
url(r'', include(router.urls)),
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/rest_framework/routers.py", line 101, in urls
self._urls = self.get_urls()
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/rest_framework/routers.py", line 363, in get_urls
urls = super(DefaultRouter, self).get_urls()
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/rest_framework/routers.py", line 261, in get_urls
routes = self.get_routes(viewset)
File "/home/wishbook/Desktop/Dev/rest/env/local/lib/python2.7/site-packages/rest_framework/routers.py", line 176, in get_routes
extra_actions = viewset.get_extra_actions()
AttributeError: 'function' object has no attribute 'get_extra_actions'
这是我的serializers.py
class CatalogSerializer(serializers.ModelSerializer):
# company_details = CompanyData(many=True)
total_pieces = serializers.SerializerMethodField()
total_price = serializers.SerializerMethodField()
# company = CompanySerializer(source='company_name', read_only=True)
# company_name = serializers.CharField()
class Meta:
model = Catalog
fields = ('name','no_of_pcs','per_piece_price','company_name','total_pieces','total_price')
#depth = 1
def to_representation(self, instance):
rep = super(CatalogSerializer, self).to_representation(instance)
rep['company'] = {
"company_name":instance.company_name.name,
"phone_number":instance.company_name.phone_number,
"company_id":instance.company_name.id
}
return rep
def get_total_pieces(self, obj):
totalpieces = Catalog.objects.aggregate(total_pieces=Count('no_of_pcs'))
return totalpieces["total_pieces"]
def get_total_price(self, obj):
totalprice = Catalog.objects.aggregate(total_price=Sum('per_piece_price'))
return totalprice["total_price"]
这是我的views.py
from rest_framework.decorators import api_view
@api_view(['GET', 'POST'])
def CatalogView(request):
if request.method == 'GET':
catalogs = Catalog.objects.select_related('company_name')
serializer = CatalogSerializer(catalogs, many=True)
return Response(serializer.data)
elif request.method == 'POST':
serializer = SnippetSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Django rest框架 为什么显示:
AttributeError:“函数”对象没有属性“ get_extra_actions”错误。
为什么显示:
AttributeError:“函数”对象没有属性“ get_extra_actions”错误。
请检查。
谢谢...