0
我有一個Character模型。我試圖用Django Rest Framework保存一個新的。surname是此函數的無效關鍵字參數
我的觀點:
def create_character(request):
existing_character = Character.objects.filter(user=request.user)
if existing_character.count() > 0:
return HttpResponse(status=400)
request.GET = request.GET.copy()
request.GET["user"] = request.user.id
serializer = CharacterSerializer(data=request.GET)
if not serializer.is_valid():
return HttpResponse(status=401, content=serializer.errors)
serializer.save()
return Response(serializer.data)
我的模型:
class Character(models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=50)
surname = models.CharField(max_length=50)
date_created = models.DateField()
age = models.SmallIntegerField()
health = models.SmallIntegerField()
happiness = models.SmallIntegerField()
depression = models.SmallIntegerField()
我的串行:
class CharacterSerializer(serializers.ModelSerializer):
health = serializers.IntegerField(required=False)
happiness = serializers.IntegerField(required=False)
depression = serializers.IntegerField(required=False)
date_created = serializers.DateField(required=False)
class Meta:
model = Character
fields = ('user', 'pk', 'name', 'surname', 'date_created', 'user', 'age', 'health', 'happiness', 'depression',
'date_created')
def create(self, validated_data):
validated_data["health"] = 100
validated_data["happiness"] = 100
validated_data["depression"] = 0
validated_data["date_created"] = date.today()
return User.objects.create(**validated_data)
當串行調用return User.objects.create(**validated_data)
我是gettine例外:
「姓」是這個函數
好吧,有時候另一雙眼睛會有很大幫助。謝謝 :) – Sefa