我有 2 个序列化程序:
class DetalleSerializer(serializers.ModelSerializer):
producto = serializers.CharField(source='producto.nombre')
class Meta:
model = DetalleVenta
fields = ('cantidad','producto')
class PedidoSerializer(serializers.ModelSerializer):
detalleventa = DetalleSerializer(many=True, read_only=True)
class Meta:
model = Venta
fields = ('id','cliente','descripcion','detalleventa','atendido')
和我的观点:
class PedidoViewSet(viewsets.ModelViewSet):
queryset = Venta.objects.exclude(atendido=True)
serializer_class = PedidoSerializer
def destroy(self, request, pk=None):
try:
queryset = Venta.objects.exclude(atendito=True)
object = get_object_or_404(queryset, pk=pk)
object.atendido = True
object.save(update_fields=['atendido'])
return Response({"status": True, "results": "Pedido atendido correctamente"})
except NotFound as err:
return Response({"status": False, "error_description": err.detail})
要删除,只需更改我的参与字段的状态,这是一个布尔(真/假)逻辑删除。y estas mis 2 个网址:
url(r'^pedido/$',PedidoViewSet.as_view({'get': 'list', 'post': 'create'}),name='api-pedido',),
url(r'^pedido/(?P<pk>\d+)/$',PedidoViewSet.as_view({'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'}),
name='api-atendido',),
所有数据的恢复都不是问题,它给我带来了我需要的一切。
通过 url: url: "{% url 'api-pedido'%}", GET
但是当我想对按钮进行逻辑删除时**(DELETE):
$('.btn').click(function(){
$.ajax({
url: "{% url 'api-atendido' %}",
data: {pk:91},
type: 'DELETE',
contentType: 'application/json',
success: function(result) {
console.log('atendido correctamente');
},
});
});
它向我显示以下错误:Reverse for 'api-atendido' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['pedido/(?P<pk>\\d+)/$']
缺少什么?还是我做错了什么?