使用 django-rest framework-generics 时如何修复 TypeError
How to fix TypeError when using django-restframework-generics
我正在学习如何使用基于 class 的通用视图,但我一直收到类型错误
from invoice.models import Invoice
from invoice.serializers import InvoiceSerializer
from rest_framework import generics
class InvoiceList(generics.ListCreateAPIView):
queryset = Invoice.objects.all()
serializer_class = InvoiceSerializer
class InvoiceDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Invoice.objects.all()
serializer_class = InvoiceSerializer
serialzers.py
from rest_framework import serializers
from .models import Invoice
class InvoiceSerializer(serializers.ModelSerializer):
class Meta:
model = Invoice
fields = "__all__"
urls.py
from django.urls import path
from invoice import views
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
path('invoice/', views.InvoiceList),
path('invoice/<int:pk>/', views.InvoiceDetail),
]
urlpatterns = format_suffix_patterns(urlpatterns)
我收到类型错误消息“init() takes 1 positional argument but 2 were given”
您在 urls.py 中的导入看起来有误。
在 urls.py 中,尝试这样的操作:
from django.urls import path
from .views import InvoiceList
path('invoice/', InvoiceList.as_view(), name="invoice"), # name can be anything you want.
我没用过format_suffix_patterns,所以你可以把那条线拿下来试试看行不行。
我正在学习如何使用基于 class 的通用视图,但我一直收到类型错误
from invoice.models import Invoice
from invoice.serializers import InvoiceSerializer
from rest_framework import generics
class InvoiceList(generics.ListCreateAPIView):
queryset = Invoice.objects.all()
serializer_class = InvoiceSerializer
class InvoiceDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Invoice.objects.all()
serializer_class = InvoiceSerializer
serialzers.py
from rest_framework import serializers
from .models import Invoice
class InvoiceSerializer(serializers.ModelSerializer):
class Meta:
model = Invoice
fields = "__all__"
urls.py
from django.urls import path
from invoice import views
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
path('invoice/', views.InvoiceList),
path('invoice/<int:pk>/', views.InvoiceDetail),
]
urlpatterns = format_suffix_patterns(urlpatterns)
我收到类型错误消息“init() takes 1 positional argument but 2 were given”
您在 urls.py 中的导入看起来有误。 在 urls.py 中,尝试这样的操作:
from django.urls import path
from .views import InvoiceList
path('invoice/', InvoiceList.as_view(), name="invoice"), # name can be anything you want.
我没用过format_suffix_patterns,所以你可以把那条线拿下来试试看行不行。