【问题标题】:Django Rest Framework - AttributeError: 'function' object has no attribute 'get_extra_actions'Django Rest Framework - AttributeError:'function'对象没有属性'get_extra_actions'
【发布时间】:2021-06-15 00:04:11
【问题描述】:

在 Django 3.2.4 和 djangorestframework 3.12.4 中出现“AttributeError: 'function' object has no attribute 'get_extra_actions'”错误

日志:

backend     | Traceback (most recent call last):
backend     |   File "manage.py", line 22, in <module>
backend     |     main()
backend     |   File "manage.py", line 18, in main
backend     |     execute_from_command_line(sys.argv)
backend     |   File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
backend     |     utility.execute()
backend     |   File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
backend     |     self.fetch_command(subcommand).run_from_argv(self.argv)
backend     |   File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
backend     |     self.execute(*args, **cmd_options)
backend     |   File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
backend     |     output = self.handle(*args, **options)
backend     |   File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
backend     |     res = handle_func(*args, **kwargs)
backend     |   File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 75, in handle
backend     |     self.check(databases=[database])
backend     |   File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 419, in check
backend     |     all_issues = checks.run_checks(
backend     |   File "/usr/local/lib/python3.8/site-packages/django/core/checks/registry.py", line 76, in run_checks
backend     |     new_errors = check(app_configs=app_configs, databases=databases)
backend     |   File "/usr/local/lib/python3.8/site-packages/django/core/checks/urls.py", line 13, in check_url_config
backend     |     return check_resolver(resolver)
backend     |   File "/usr/local/lib/python3.8/site-packages/django/core/checks/urls.py", line 23, in check_resolver
backend     |     return check_method()
backend     |   File "/usr/local/lib/python3.8/site-packages/django/urls/resolvers.py", line 412, in check
backend     |     for pattern in self.url_patterns:
backend     |   File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
backend     |     res = instance.__dict__[self.name] = self.func(instance)
backend     |   File "/usr/local/lib/python3.8/site-packages/django/urls/resolvers.py", line 598, in url_patterns
backend     |     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
backend     |   File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
backend     |     res = instance.__dict__[self.name] = self.func(instance)
backend     |   File "/usr/local/lib/python3.8/site-packages/django/urls/resolvers.py", line 591, in urlconf_module
backend     |     return import_module(self.urlconf_name)
backend     |   File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
backend     |     return _bootstrap._gcd_import(name[level:], package, level)
backend     |   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
backend     |   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
backend     |   File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
backend     |   File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
backend     |   File "<frozen importlib._bootstrap_external>", line 848, in exec_module
backend     |   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
backend     |   File "/app/backend/urls.py", line 24, in <module>
backend     |     path('api/properties/', include('apps.properties.urls')),
backend     |   File "/usr/local/lib/python3.8/site-packages/django/urls/conf.py", line 34, in include
backend     |     urlconf_module = import_module(urlconf_module)
backend     |   File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
backend     |     return _bootstrap._gcd_import(name[level:], package, level)
backend     |   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
backend     |   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
backend     |   File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
backend     |   File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
backend     |   File "<frozen importlib._bootstrap_external>", line 848, in exec_module
backend     |   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
backend     |   File "/app/apps/properties/urls.py", line 14, in <module>
backend     |     urlpatterns = router.urls
backend     |   File "/usr/local/lib/python3.8/site-packages/rest_framework/routers.py", line 77, in urls
backend     |     self._urls = self.get_urls()
backend     |   File "/usr/local/lib/python3.8/site-packages/rest_framework/routers.py", line 236, in get_urls
backend     |     routes = self.get_routes(viewset)
backend     |   File "/usr/local/lib/python3.8/site-packages/rest_framework/routers.py", line 152, in get_routes
backend     |     extra_actions = viewset.get_extra_actions()
backend     | AttributeError: 'function' object has no attribute 'get_extra_actions'

views.py

from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response

from .models import Property
from .serializers import PropertySerializer

def PropertiesViewSet(ModelViewSet):
    queryset = Property.objects.all()
    serializer_class = PropertySerializer

urls.py

from rest_framework import routers
from .views import PropertiesViewSet

router = routers.SimpleRouter()

router.register('properties', PropertiesViewSet, basename='properties')

urlpatterns = router.urls

serializers.py

from rest_framework import serializers
from .models import Property

class PropertySerializer(serializers.ModelSerializer):
   class Meta:
       model = Property
       fields = '__all__'

models.py

from uuid import uuid4
from django.db import models


class Property(models.Model):

    uuid = models.UUIDField(
        primary_key=True,
        default=uuid4,
        editable=False
    )

    property_address = models.CharField(max_length=128, unique=True)

    host_uuid = models.UUIDField(
        primary_key=False,
        default=uuid4,
        editable=False
    )

    current_certificate_uuid = models.UUIDField(
        primary_key=False,
        default=uuid4,
        editable=False
    )

    created_at = models.DateField(auto_now_add=True)

    is_active = models.BooleanField()

    property_type = models.CharField(max_length=128)

    description = models.TextField(max_length=256)

    amenities = models.JSONField()

    total_bedrooms = models.PositiveSmallIntegerField()

    longitude = models.DecimalField(
        max_digits=10,
        decimal_places=7
    )

    latitude = models.DecimalField(
        max_digits=9,
        decimal_places=7
    )

    max_guests = models.PositiveSmallIntegerField()
    

    def __str__(self):
        return self.property_address

    class Meta:
        verbose_name = "property"
        verbose_name_plural = "properties"

我搜索了所有其他问题,其中大多数都使用 APIViews 作为 ViewSets,但这里并非如此。我也尝试添加 get_extra_actions 函数(返回 []),但它没有检测到它。

谢谢!

【问题讨论】:

    标签: python python-3.x django django-rest-framework


    【解决方案1】:

    这是一个错字:

    
    def PropertiesViewSet(ModelViewSet):
        queryset = Property.objects.all()
        serializer_class = PropertySerializer
    
    
    # Should be class
    
    class PropertiesViewSet(ModelViewSet):
        queryset = Property.objects.all()
        serializer_class = PropertySerializer
    

    【讨论】:

      【解决方案2】:

      在您的urls.py 中尝试设置urlpatterns 之类的

      from django.urls import path, include
      from rest_framework.routers import DefaultRouter
      from .views import PropertiesViewSet
      
      router = DefaultRouter()
      
      router.register('properties', PropertiesViewSet, basename='properties')
      
      urlpatterns = [
          path('', include(router.urls)),
      ]
      
      

      来自The Tutorial

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-26
      • 2018-11-12
      • 2022-01-19
      • 2021-07-15
      • 1970-01-01
      • 2021-01-12
      • 2020-07-03
      • 1970-01-01
      相关资源
      最近更新 更多