Django REST Framework 创建自定义用户 [英] Django REST Framework Creating custom user

查看:35
本文介绍了Django REST Framework 创建自定义用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Django 领域的新手,但看到那里有很多魔法".我正在使用 Django REST Framework 并创建允许免费用户注册的应用程序.我的用户需要一些在 Django 用户中不可用的附加字段.所以我用谷歌搜索扩展用户.有一种想法应该通过创建这样的东西来完成

I'm new in Django realm but see there is a lot of "magic" there. I'm using Django REST Framework and creating app that will allow free user registration. My user needs some additional fields that are not available in Django user. So I googled for extending User. There is an idea that this should be done by creating something like this

class MyUser(models.Model):
    user = models.ForeignKey(User, unique=True)
    city = models.CharField(max_length=50, blank=True, default='')

这很好,但我有这个序列化器

This is fine but I have this serializer

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = MyUser
        fields = ('id', 'username', 'password', 'first_name', 'last_name', 'email', 'city')

所以,问题是这个序列化器在这里做了一些魔法".它试图弄清楚模型应该具有什么字段......我想让用户在这里列出字段,这些字段在用户中,城市"是新的自定义字段.序列化程序不知道它应该查看 User 模型内部.

So, the problem is that this serializer does some "magic" here. It tries to figure out what field should model have ... I want to have user with fields listed here, and these are fields are in User and 'city' is new custom field. Serializer doesn't get that it should look inside User model.

我在这里错过了什么?如何告诉这个序列化程序我想要用户内部的一些字段?我也需要能够 crete 用户.

What am I missing here? How to tell this serializer that I want some fields inside User? I need to be able to crete user too.

推荐答案

所以显然我没有足够的声誉在答案下发表评论.但是要详细说明 Kevin Stone 所描述的内容,如果您的模型类似于以下内容:

So apparently I don't have enough reputation to post a comment under an answer. But to elaborate on what Kevin Stone described, if you model is something like the following:

class AppUser(models.Model):
    user = models.OneToOneField(User)
    ban_status = models.BooleanField(default=False)

您可以执行以下操作来创建自定义用户和 django 用户:

You can do something like this to create both the custom user and django user:

class AppUserSerializer(serializers.ModelSerializer):
    username = serializers.CharField(source='user.username')
    email = serializers.CharField(source='user.email')
    password = serializers.CharField(source='user.password')
    ban_status = serializers.Field(source='ban_status')

    class Meta:
        model = AppUser
        fields = ('id', 'username', 'email', 'password', 'ban_status')

    def restore_object(self, attrs, instance=None):
        """
        Given a dictionary of deserialized field values, either update
        an existing model instance, or create a new model instance.
        """
        if instance is not None:
            instance.user.email = attrs.get('user.email', instance.user.email)
            instance.ban_status = attrs.get('ban_status', instance.ban_status)
            instance.user.password = attrs.get('user.password', instance.user.password)
            return instance

        user = User.objects.create_user(username=attrs.get('user.username'), email= attrs.get('user.email'), password=attrs.get('user.password'))
        return AppUser(user=user)

这篇关于Django REST Framework 创建自定义用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆