使用 django-simple-history 和 DRF 时,如何创建额外的操作来访问对象的历史记录?
When using django-simple-history and DRF, how can I create an extra action to access the history of an object?
我是 Python、Django 和 DRF 的新手,但我已经完成了设置基本 REST 服务器所需的所有教程,对于这个例子,我们假设它只服务于一个事情:客户名单。此外,我已经安装了 django-simple-history
并用它注册了我的 Customer
模型。
这是我到目前为止编写的代码,一切似乎都运行良好。
api/urls.py
from django.urls import path, include
from rest_framework import routers
import api.views as views
router = routers.DefaultRouter(trailing_slash=False)
router.register('customers', views.CustomerView)
router.register('customerhistory', views.CustomerHistoryView)
urlpatterns = [
path('', include(router.urls)),
]
api/views.py
from rest_framework import viewsets
from retail.models.customers import Customer
class CustomerView(viewsets.ModelViewSet):
queryset = Customer.objects.all()
serializer_class = CustomerSerializer
filterset_fields = '__all__'
search_fields = ['name']
class CustomerHistoryView(viewsets.ModelViewSet):
queryset = Customer.history.all()
serializer_class = CustomerHistorySerializer
filterset_fields = '__all__'
api/serializers.py
from rest_framework import serializers
from retail.models.customers import Customer
class CustomerSerializer(serializers.ModelSerializer):
class Meta:
model = Customer
fields = '__all__'
class CustomerHistorySerializer(serializers.ModelSerializer):
class Meta:
model = Customer.history.model
fields = '__all__'
实际上,当我转到 api/customerhistory/1
时,它为我提供了客户 1 的所有历史记录的可爱列表。
问题
我想做的是通过转到 api/customer/1/history
.
访问相同的列表
到目前为止我的尝试...
为此,我将删除 CustomerHistoryView
并向 CustomerView
class 添加额外的操作。
api/views.py
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from retail.models.customers import Customer
class CustomerView(viewsets.ModelViewSet):
queryset = Customer.objects.all()
serializer_class = CustomerSerializer
filterset_fields = '__all__'
search_fields = ['name']
@action(detail=True)
def history(self):
history = Customer.history.all()
serializer = CustomerHistorySerializer(history, many=True)
return Response(serializer.data)
这会产生以下错误:
Exception Type: TypeError at /api/customers/1/history
Exception Value: history() got multiple values for argument 'pk'
我做错了什么?
您的 history
方法似乎缺少参数。
Here are the docs for extra actions on viewsets.
@action(detail=True)
def history(self, request, pk=None):
history = Customer.history.all()
# ...
注意额外的参数 request
和 pk=None
到 history
。
我是 Python、Django 和 DRF 的新手,但我已经完成了设置基本 REST 服务器所需的所有教程,对于这个例子,我们假设它只服务于一个事情:客户名单。此外,我已经安装了 django-simple-history
并用它注册了我的 Customer
模型。
这是我到目前为止编写的代码,一切似乎都运行良好。
api/urls.py
from django.urls import path, include
from rest_framework import routers
import api.views as views
router = routers.DefaultRouter(trailing_slash=False)
router.register('customers', views.CustomerView)
router.register('customerhistory', views.CustomerHistoryView)
urlpatterns = [
path('', include(router.urls)),
]
api/views.py
from rest_framework import viewsets
from retail.models.customers import Customer
class CustomerView(viewsets.ModelViewSet):
queryset = Customer.objects.all()
serializer_class = CustomerSerializer
filterset_fields = '__all__'
search_fields = ['name']
class CustomerHistoryView(viewsets.ModelViewSet):
queryset = Customer.history.all()
serializer_class = CustomerHistorySerializer
filterset_fields = '__all__'
api/serializers.py
from rest_framework import serializers
from retail.models.customers import Customer
class CustomerSerializer(serializers.ModelSerializer):
class Meta:
model = Customer
fields = '__all__'
class CustomerHistorySerializer(serializers.ModelSerializer):
class Meta:
model = Customer.history.model
fields = '__all__'
实际上,当我转到 api/customerhistory/1
时,它为我提供了客户 1 的所有历史记录的可爱列表。
问题
我想做的是通过转到 api/customer/1/history
.
到目前为止我的尝试...
为此,我将删除 CustomerHistoryView
并向 CustomerView
class 添加额外的操作。
api/views.py
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from retail.models.customers import Customer
class CustomerView(viewsets.ModelViewSet):
queryset = Customer.objects.all()
serializer_class = CustomerSerializer
filterset_fields = '__all__'
search_fields = ['name']
@action(detail=True)
def history(self):
history = Customer.history.all()
serializer = CustomerHistorySerializer(history, many=True)
return Response(serializer.data)
这会产生以下错误:
Exception Type: TypeError at /api/customers/1/history
Exception Value: history() got multiple values for argument 'pk'
我做错了什么?
您的 history
方法似乎缺少参数。
Here are the docs for extra actions on viewsets.
@action(detail=True)
def history(self, request, pk=None):
history = Customer.history.all()
# ...
注意额外的参数 request
和 pk=None
到 history
。