可以Django restframework generics.ListCreateAPIView处理一个
我有一个模型UserProfile
,它有一个与Django用户模型相关的OneToOneField
我在view.py中有UserProfileList
class UserProfileList(generics.ListCreateAPIView):
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
我想要将数据发布到UserProfileList
格式如下:
{ "username":"username",
"email":"email@email",
"password":"password",
"secret_id":1
}
('用户名','电子邮件','密码'来自User
模型secret_id来自UserProfile
)
它可以将数据保存在User
和UserProfile
表中
它是否是posibble?
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
secret_id=models.IntegerField(default=0)
views.py
from rest_framework import generics
from django.contrib.auth.models import User
from .serializers import UserProfileSerializer, UserSerializer
from account.models import UserProfile
class UserList(generics.ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
class UserDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
class UserProfileList(generics.ListCreateAPIView):
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
class UserProfileDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
serializer.py
from django.contrib.auth.models import User
from rest_framework import serializers
from account.models import UserProfile
class UserProfileSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = UserProfile
fields = ('user','secret_id')
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('username','email','password')
是的,这是可能的。
为了能够访问UserProfileSerializer
的username
, email
和password
字段,您需要在其上定义这些字段,否则,序列化程序的验证将会抛出没有在序列化程序中定义的字段:
class UserProfileSerializer(serializers.HyperLinkedModelSerializer):
email = serializers.CharField(write_only=True)
username = serializers.CharField(write_only=True)
password = serializers.CharField(write_only=True)
class Meta:
...
请注意,显式定义的字段是write_only
,因此在读取时它们不会显示出来。
上面的代码会向你保证这些字段不会从你的序列化器的.create()
和.update()
方法中的validated_attrs
中消除。
之后你有2个选项
选项1
您需要覆盖UserProfileSerializer
的.create()
方法,以创建除UserProfile
模型以外的User
模型。
class UserProfileSerializer(serializers.HyperLinkedModelSerializer):
# above definitions
def create(self, validated_attrs):
# now you have here the email, username and password arguments
email = validated_attrs.pop('email', None)
username = validated_attrs.pop('username', None)
password = validated_attrs.pop('password', None)
# your creation logic here
user = User.objects.create(...)
user_profile = UserProfile.objects.create(user=user, ...)
选项#2
您可以在User
创建过程中处理UserProfile
的创建(请参阅处理在模型管理器类中保存相关实例)。 为此,您需要定义一个自定义的UserManager
类。
class UserManager(models.Manager):
...
def create(self, username, email, secret_id=None):
user = User(username=username, email=email)
user.save()
user_profile = UserProfile(
user=user,
secret_id=secret_id
)
user_profile.save()
return user
链接地址: http://www.djcxy.com/p/33745.html
上一篇: Can django restframework generics.ListCreateAPIView deal with one