Я пытаюсь объединить два входных json данных для класса django представления структуры отдыха.
Я загружаю данные в этом формате
{
"profile": {
"phone": "9841101115",
"email": "nirjalpaudl543sddsd12@gmail.com",
"bio": "I am the admin of the site",
"status": "AWY",
"address": "Pepsicola, Kathmandu"
},
"user": {
"username": "sampanna12",
"password1": "test12345",
"password2": "test12345"
}
}
Показанная ошибка предполагает, что данные никогда не были получены сериализаторами. Я получаю ответ
{
"Error": "Something went wrong while making a user",
"message": {
"username": [
"This field is required."
],
"password1": [
"This field is required."
],
"password2": [
"This field is required."
]
}
}
Мое представление сообщения получает данные, но моя форма не получает, как было предложено в ответе введите описание изображения здесь Мой models.py выглядит как
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE,related_name="profile_user")
email = models.EmailField(max_length=50,blank=False,unique=True)
bio = models.TextField(verbose_name="Describe Yourself")
statustypes = (
("LFJ","Looking for job"),
("CAJ","Creating a job"),
("AWY","Away for somtime"),
)
status = models.CharField(choices=statustypes,max_length=3,blank=False)
phone = models.CharField(max_length=15,unique=True)
address = models.CharField(max_length=40,)
#cv = models.OneToOneField(CV,on_delete=models.CASCADE)
#pp = models.ImageField(verbos_name="Profile Picture",upload_to="profile pictures")
def __str__(self):
return self.user.username
Мой serializer.py выглядит как
from rest_framework import serializers
from django.contrib.auth.models import User
from . import models
class UserSerializer(serializers.ModelSerializer):
password1=serializers.CharField(
write_only=True,
min_length=8,
required=True,
style={
"input_type":"password"
}
)
password2=serializers.CharField(
write_only=True,
min_length=8,
required=True,
style={
"input_type":"password"
}
)
class Meta:
model=User
fields=["username","password1","password2"]
class ProfileSerializer(serializers.ModelSerializer):
user=UserSerializer()
class Meta:
model=models.Profile
fields=['phone','email','bio','status','address','user']
Мой views.py выглядит как
from django.shortcuts import render
from rest_framework.decorators import APIView
from django.contrib.auth.models import User
from rest_framework.response import Response
from . import serializers,models
# Create your views here.
class UserApi(APIView):
def get(self,request, format=None):
allusers=models.Profile.objects.all()
print(allusers)
data=serializers.ProfileSerializer(allusers,many=True)
return Response(data.data)
def post(self,request, format=None):
data=request.data
print(data.get("user"))
print(data.get("profile"))
sampleprofile=serializers.ProfileSerializer( data=data.get("user") )
sampleuser=serializers.UserSerializer( data=data.get("profile") )
if sampleuser.is_valid():
currentuser=sampleuser.create()
else:
return Response({
"Error":"Something went wrong while making a user",
"message":sampleuser.errors,
})
sampleprofile.user=sampleuser
if sampleprofile.is_valid():
sampleprofile.create()
else:
return Response({
"Error":"Something went wrong while making profile",
"message":sampleprofile.errors,
})