Django Rest Framework创建用户和用户配置文件

时间:2015-11-11 21:15:08

标签: python django django-rest-framework

我正在尝试创建将使用我的API创建新用户的视图。 我正在为我的用户使用自定义模型,还创建了一个名为Profile的模型来管理与身份验证无关的数据。

我是Django世界的新手,这可能非常困难。

这是我的models.py

class UserManager(BaseUserManager):
    def create_user(self, email, password=None):
        if not email:
            raise ValueError('User must have an email address')

        user = self.model(
            email = self.normalize_email(email),
        )
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, email, password):
        user = self.create_user(email, password=password)
        user.is_admin = True
        user.save()
        return user


class User(AbstractBaseUser):
    objects = UserManager()
    email = models.EmailField(unique=True, db_index=True)
    created = models.DateTimeField('created', auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    is_active = models.BooleanField('active', default=True)
    is_admin = models.BooleanField('admin', default=False)

    USERNAME_FIELD = 'email'

    ordering = ('created',)

    def is_staff(self):
        return self.is_admin

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    def get_short_name(self):
        return self.email

    def get_full_name(self):
        return self.email

    def __unicode__(self):
        return self.email


class Profile(models.Model):
    GENDER = (
        ('M', 'Homme'),
        ('F', 'Femme'),
    )

    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    first_name = models.CharField(max_length=120, blank=False)
    last_name = models.CharField(max_length=120, blank=False)
    gender = models.CharField(max_length=1, choices=GENDER)
    zip_code = models.CharField(max_length=5, validators=[MinLengthValidator(5)], blank=False)

    def __unicode__(self):
        return u'Profile of user: {0}'.format(self.user.email)


def create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)
post_save.connect(create_profile, sender=User)


def delete_user(sender, instance=None, **kwargs):
    try:
        instance.user
    except User.DoesNotExist:
        pass
    else:
        instance.user.delete()
post_delete.connect(delete_user, sender=Profile)

这是我的serializers.py

class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profile
        fields = ('first_name', 'last_name', 'gender', 'zip_code',)


class UserSerializer(serializers.ModelSerializer):
    profile = ProfileSerializer(required=True)
    class Meta:
        model = User
        fields = ('url', 'email', 'profile', 'created',)

这是views.py

class UserList(generics.ListCreateAPIView):
    permission_classes = (IsAuthenticatedOrWriteOnly,)
    serializer_class = UserSerializer

    def get_queryset(self):
        if self.request.user.is_staff:
            return User.objects.all()
        else:
            return self.request.user

    def post(self, request, format=None):
        serializer = ProfileSerializer(data=request.data)
        print serializer.__dict__
        if serializer.is_valid():
            print "valid"
        return Response("placeholder", status=status.HTTP_201_CREATED)

我的目标是能够同时创建用户和个人资料,同时能够有效地完成所有事情,我该如何实现?

2 个答案:

答案 0 :(得分:10)

事实上,Django Rest Framework无法通过嵌套关系处理这项工作,那么您必须自己实现这些方法。我将举例说明您的代码应该是什么样的。

您的观点:

class UserList(generics.ListCreateAPIView):
    permission_classes = (IsAuthenticatedOrWriteOnly,)
    serializer_class = UserSerializer

    def post(self, request, format=None):
        serializer = UserSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

现在,当您想要更新对象时,如果要创建对象和save方法,则序列化程序的create方法将调用update方法。因此,让我们实施create的{​​{1}}方法,该方法将创建配置文件和用户。以下是UserSerializer应该是什么样子的简单示例:

UserSerializer

正如我所说,这是一个例子,你必须完成它才能做你想做的事,但现在,你知道该怎么做:)要在更新期间定义行为,实现class UserSerializer(serializers.ModelSerializer): profile = ProfileSerializer(required=True) class Meta: model = User fields = ('url', 'email', 'profile', 'created',) def create(self, validated_data): # create user user = User.objects.create( url = validated_data['url'], email = validated_data['email'], # etc ... ) profile_data = validated_data.pop('profile') # create profile profile = Profile.objects.create( user = user first_name = profile_data['first_name'], last_name = profile_data['last_name'], # etc... ) return user 方法

答案 1 :(得分:1)

这可能有点晚了,但为什么不完全绕过 drf 并将配置文件创建触发器委托给使用如下信号的模型:

models.py

class User(AbstractBaseUser):
      pass
class UserProfile(models.Model):
      user =  models.OneToOneField(User, on_delete=models.CASCADE, related_name='customer_profile')
      country = models.CharField(blank=True, max_length=250)

信号.py

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(
            user = instance
        )

一旦通过任何方式创建用户,这基本上会触发相应的配置文件行创建;管理员、drf 等

然后您可以使用序列化程序来更新数据。 希望这可以帮助其他在这里磕磕绊绊的人