Can django restframework generics.ListCreateAPIView deal with one

I have a model UserProfile which has a OneToOneField related to Django User model
And I have UserProfileList in views.py

class UserProfileList(generics.ListCreateAPIView):
        queryset = UserProfile.objects.all()
        serializer_class = UserProfileSerializer

I want to post data to UserProfileList
The format like :

{   "username":"username",
    "email":"email@email",
    "password":"password",
    "secret_id":1
}

('username','email','password' is from User model secret_id is from UserProfile )

and it can save data both in User and UserProfile table
Is it 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')

Yes, it is possible to achieve this.

In order to have access to the username , email and password fields from within the UserProfileSerializer , you need to define these fields on it, otherwise, the serializer's validation will throw out fields that are not defined on the serializer:

class UserProfileSerializer(serializers.HyperLinkedModelSerializer):
    email = serializers.CharField(write_only=True)
    username = serializers.CharField(write_only=True)
    password = serializers.CharField(write_only=True)

    class Meta:
        ...

Notice that the explicitly defined fields are write_only , so that on read they won't show up.

The above code will assure you that these fields won't be eliminated from validated_attrs in your serializer's .create() and .update() methods.

After this you have 2 options

Option #1

You need to overwrite the .create() method of your UserProfileSerializer in order to create your User model besides the UserProfile model.

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, ...)

Option #2

You can handle the creation of UserProfile from within the User creation process (see handling saving related instances in model manager classes). For this you need to define a custom UserManager class.

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/33746.html

上一篇: 身份验证方法在django中返回无

下一篇: 可以Django restframework generics.ListCreateAPIView处理一个