类型不正确。预期pk值,已收到字典。 Django Rest Framwork-Vue

时间:2018-11-13 06:45:24

标签: python django vue.js

我是Django和Django Rest Framework的新手。我正在尝试做一个小项目来提高自己的技能,但是我被困在这里很多次。

ViewSets.py

from rest_framework import viewsets, filters
from .models import Plazo, Perfil, Solicitud, Estatus
from .serializers import (PlazoSerializer, PerfilSerializer, 
SolicitudSerializer, EstatusSerializer, 
SolicitudExpandeSerializer)


class PlazoViewSet(viewsets.ModelViewSet):
    queryset = Plazo.objects.all()
    serializer_class = PlazoSerializer


 class EstatusViewSet(viewsets.ModelViewSet):
     queryset = Estatus.objects.all()
     serializer_class = EstatusSerializer


 class PerfilViewSet(viewsets.ModelViewSet):
     queryset = Perfil.objects.all()
     serializer_class = PerfilSerializer


class SolicitudViewSet(viewsets.ModelViewSet):
    queryset = Solicitud.objects.all()
    serializer_class = SolicitudSerializer

    def get_serializer_class(self):
        serializer_class = self.serializer_class

        if self.action in ['list', 'retrieve']:
            serializer_class = SolicitudExpandeSerializer

        return serializer_class

Serializers.py

from rest_framework import serializers
from .models import Plazo, Perfil, Solicitud, Estatus


class PlazoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Plazo
        fields = '__all__'


class EstatusSerializer(serializers.ModelSerializer):
    class Meta:
        model = Estatus
        fields = '__all__'


class PerfilSerializer(serializers.ModelSerializer):
    class Meta:
        model = Perfil
        fields = '__all__'


class SolicitudSerializer(serializers.ModelSerializer):

    class Meta:
        model = Solicitud
        fields = '__all__'


class SolicitudExpandeSerializer(SolicitudSerializer):

    perfil = PerfilSerializer(many=False, read_only=False)
    status = EstatusSerializer(many=False, read_only=False)

Models.py

from django.db import models


class Plazo(models.Model):
    plazo_id = models.AutoField(primary_key=True)
    nombre = models.CharField(max_length=100)
    meses = models.IntegerField()
    intereses = models.IntegerField()

    def __str__(self):
        return self.nombre

    class Meta:
        verbose_name = 'plazo'
        verbose_name_plural = 'plazos'


class Perfil(models.Model):
    perfil_id = models.AutoField(primary_key=True)
    nombre = models.CharField(max_length=250)

    def __str__(self):
        return self.nombre

    class Meta:
        verbose_name = 'perfil'
        verbose_name_plural = 'perfiles'


class Estatus(models.Model):
    nombre = models.CharField(max_length=250)

    def __str__(self):
        return str(self.nombre)

    class Meta:
        verbose_name = 'estatus'
        verbose_name_plural = 'estatus'


class Solicitud(models.Model):

    ACEPTADO, PENDIENTE, RECHAZADO = range(1, 4)

    ESTADOS = (
        (ACEPTADO, 'Aceptado'),
        (PENDIENTE, 'Pendiente'),
        (RECHAZADO, 'Rechazado')
    )

    solicitud_id = models.AutoField(primary_key=True)
    monto = models.IntegerField()
    tarjeta_de_credito = models.BooleanField(default=False)
    plazo = models.ForeignKey(Plazo, related_name='SolicitudPlazo', blank=True, null=True, on_delete=models.PROTECT)
    perfil = models.ForeignKey(Perfil, related_name='SolicitudPerfil', blank=True, null=True, on_delete=models.PROTECT)
    status = models.ForeignKey(Estatus, related_name='SolicitudEstatus', blank=True, null=True, on_delete=models.PROTECT)
    total = models.IntegerField(blank=True, null=True)
    mostrar = models.BooleanField(default=True)

    def __str__(self):
        return str(self.solicitud_id)

    class Meta:
        verbose_name = 'solicitud'
        verbose_name_plural = 'solicitudes'

JS

    updateSolicitud: function() {
     this.loading = true;
     this.$http.put(`/api/solicitud/${this.currentSolicitud.solicitud_id}/`, 
     this.currentSolicitud)
            .then((response) => {
              this.loading = false;
              this.currentSolicitud = response.data;
              this.getSolicitudes();
            })
            .catch((err) => {
              this.loading = false;
              console.log(err);
            })
      }

当我尝试更新记录时,出现此错误。我尝试过从输入传递“ Perfil”值,但它仍然无法识别并创建字典。

  

“ {” perfil“:[”类型错误。预期的pk值,已收到字典。“]”状态“:[”类型错误。预期的pk值,已收到字典。“]}”

2 个答案:

答案 0 :(得分:1)

您的ProfileSerializer需要一个数字,特别是您要更新的对象的“ pk”,此刻您正在将json发送到具有字典的服务器。

您的服务器希望使用这种格式的json来更新配置文件。

{   “ id”:“ 1”,(或您要更新的任何ID   “ nombre”:“您的个人资料的新名称”, }

我建议您检查要发送到服务器的json。

答案 1 :(得分:0)

仅将其放在此处,因为这是搜索结果的顶部,但是我遇到的情况是我有一个嵌套序列化器

我能够通过将字段名称添加到exclude类的Meta属性来解决此问题,因为我不需要为特定的序列化程序将该字段的结果序列化

例如:

class SolicitudSerializer(serializers.ModelSerializer):

    class Meta:
        model = Solicitud
        exclude = ('field_name', )