我在DRF 3.1上遇到这个奇怪的错误:
init ()获得了意外的关键字参数' allow_blank'
我认为它来自我的serializers.py文件,但我不知道为什么。
这是我的代码
models.py
from django.db import models
# Create your models here.
class Expediteur(models.Model):
nom = models.CharField(max_length=50)
prenom = models.CharField(max_length=100)
adresse = models.CharField(max_length=200)
tel = models.IntegerField()
class Destinataire(models.Model):
nom = models.CharField(max_length=50)
prenom = models.CharField(max_length=100)
adresse = models.CharField(max_length=200)
tel= models.IntegerField()
class LettrePrio(models.Model):
expediteur = models.ForeignKey(Expediteur)
destinataire = models.ForeignKey(Destinataire)
prix = models.FloatField('0,76')
class TypeLettreReco(models.Model):
R1 = models.FloatField('4,72')
R2 = models.FloatField('5,40')
R3 = models.FloatField('6,40')
class LettreReco(models.Model):
expediteur = models.ForeignKey(Expediteur)
destinataire = models.ForeignKey(Destinataire)
prix = models.ForeignKey(TypeLettreReco)
class TimbrePrio(models.Model):
expediteur = models.ForeignKey(Expediteur)
destinataire = models.ForeignKey(Destinataire)
lettre = models.ForeignKey(LettrePrio)
class TimbreReco(models.Model):
expediteur = models.ForeignKey(Expediteur)
destinataire = models.ForeignKey(Destinataire)
lettre = models.ForeignKey(LettreReco)
serializers.py
from rest_framework import serializers
from polls.models import Expediteur, Destinataire, LettrePrio, TypeLettreReco, LettreReco, TimbrePrio, TimbreReco
class ExpediteurSerializer(serializers.Serializer):
nom = serializers.CharField(required=True, allow_blank=False, max_length=50)
prenom = serializers.CharField(required=True, allow_blank=False, max_length=100)
adresse = serializers.CharField(required=True, allow_blank=False, max_length=200)
tel = serializers.IntegerField(required=True, allow_blank=False,)
def create(self, validated_data):
return Expediteur.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.nom = validated_data.get('nom', instance.nom)
instance.prenom = validated_data.get('prenom', instance.prenom)
instance.adresse = validated_data.get('adresse', insatnce.adresse)
instance.tel = validated_data.get('tel', instance.tel)
instance.save()
return instance
class DestinataireSerializer(serializers.Serializer):
nom = serializers.CharField(required=True, allow_blank=False, max_length=50)
prenom = serializers.CharField(required=True, allow_blank=False, max_length=100)
adresse = serializers.CharField(required=True, allow_blank=False, max_length=200)
tel = serializers.IntegerField(required=True, allow_blank=False,)
def create(self, validated_data):
return Destinataire.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.nom = validated_data.get('nom', instance.nom)
instance.prenom = validated_data.get('prenom', instance.prenom)
instance.adresse = validated_data.get('adresse', insatnce.adresse)
instance.tel = validated_data.get('tel', instance.tel)
instance.save()
return instance
class LettrePrioSerializer(serializers.Serializer):
expediteur = serializers.ForeignKey(ExpediteurSerializer)
destinataire = serializers.ForeignKey(DestinataireSerializer)
prix = serializers.FloatField(default='0,76')
def create(self, validated_data):
return LettrePrio.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.expediteur = validated_data.get('expediteur', instance.expediteur)
instance.destinataire = validated_data.get('destinataire', instance.destinataire)
instance.prix = validated_data.get('prix', instance.prix)
instance.save()
return instance
class TypeLettreRecoSerializer(serializers.Serializer):
R1 = serializers.FloatField(default='4,72')
R2 = serializers.FloatField(default='5,40')
R3 = serializers.FloatField(default='6,40')
def create(self, validated_data):
return TypeLettreReco.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.R1 = validated_data.get('R1', instance.R1)
instance.R2 = validated_data.get('R2', instance.R2)
instance.R3 = validated_data.get('R3', instance.R2)
instance.save()
return instance
class LettreRecoSerializer(serializers.Serializer):
expediteur = serializers.ForeignKey(ExpediteurSerializer)
destinataire = serializers.ForeignKey(DestinataireSerializer)
prix = serializers.ForeignKey(TypeLettreRecoSerializer)
def create(self, validated_data):
return LettreReco.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.expediteur = validated_data.get('expediteur', instance.expediteur)
instance.destinataire = validated_data.get('destinataire', instance.destinataire)
instance.prix = validated_data.get('prix', instance.prix)
instance.save()
return instance
class TimbrePrioSerializer(serializers.Serializer):
expediteur = serializers.ForeignKey(ExpediteurSerializer)
destinataire = serializers.ForeignKey(DestinataireSerializer)
lettre = serializers.ForeignKey(LettrePrioSerializer)
def create(self, validated_data):
return TimbrePrio.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.expediteur = validated_data.get('expediteur', instance.expediteur)
instance.destinataire = validated_data.get('destinataire', instance.destinataire)
instance.lettre = validated_data.get('lettre', instance.lettre)
instance.save()
return instance
class TimbreRecoSerializer(serializers.Serializer):
expediteur = serializers.ForeignKey(ExpediteurSerializer)
destinataire = serializers.ForeignKey(DestinataireSerializer)
lettre = serializers.ForeignKey(LettreRecoSerializer)
def create(self, validated_data):
return TimbreReco.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.expediteur = validated_data.get('expediteur', instance.expediteur)
instance.destinataire = validated_data.get('destinataire', instance.destinataire)
instance.lettre = validated_data.get('lettre', instance.lettre)
instance.save()
return instance
views.py
from django.shortcuts import render
from rest_framework import viewsets
from polls.serializers import ExpediteurSerializer, DestinataireSerializer, LettrePrioSerializer, TypeLettreRecoSerializer, LettreRecoSerializer, TimbrePrioSerializer, TimbreRecoSerializer
# Create your views here.
class ExpeViewSet(viewsets.ModelViewSet):
queryset = Expediteur.objects.all()
serializer_class = ExpediteurSerializer
class DestiViewSet(viewsets.ModelViewSet):
queryset = Destinataire.objects.all()
serializer_class = DestinataireSerializer
class LetPrioViewSet(viewsets.ModelViewSet):
queryset = LettrePrio.objects.all()
serializer_class = LettrePrioSerializer
class TypLetRecoViewSet(viewsets.ModelViewSet):
queryset = TypeLettreReco.objects.all()
serializer_class = TypeLettreRecoSerializer
class LetRecoViewSet(viewsets.ModelViewSet):
queryset = LettreReco.objects.all()
serializer_class = LettreRecoSerializer
class TimbrePrioViewSet(viewsets.ModelViewSet):
queryset = TimbrePrio.objects.all()
serializer_class = TimbrePrioSerializer
class TimbreRecoViewSet(viewsets.ModelViewSet):
queryset = TimbreReco.objects.all()
serializer_class = TimbreRecoSerializer
这是我的第一个DRF,所以如果错误来自我在使用DRF时无法理解的事情,我不会感到惊讶。
答案 0 :(得分:2)
tel = serializers.IntegerField(required=True, allow_blank=False,)
IntegerField不接受allow_blank
。