Django使用两个用户配置文件模型注册api(Django register api with two User profile models)

编程入门 行业动态 更新时间:2024-10-19 04:26:52
Django使用两个用户配置文件模型注册api(Django register api with two User profile models)

我正在尝试创建用户,其中用户模型有两个用户配置文件,例如教师和学生。 在创建用户时,我还想在Teacher或Student配置文件中创建User实例。

这是我的串行器类:

class StudentSerializer(serializers.ModelSerializer): class Meta: model = Student fields = ('id', 'user_id') class TeacherSerializer(serializers.ModelSerializer): class Meta: model = Teacher fields = ('id', 'user_id') class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ( 'id', 'username', 'password', 'email', 'user_type' ) extra_kwargs = { 'password': {'write_only': True}, } def create(self, validated_data): user = User.objects.create_user(**validated_data) return user

这是自定义寄存器视图:

class CreateUserView(CreateAPIView): model = User permission_classes = [ permissions.AllowAny # Or anon users can't register ] serializer_class = UserSerializer

现在,我可以创建用户,但根据user_type参数创建用户配置文件对我来说是一个挑战。

所以,如果user_type是student ,我还需要为StudentProfile创建User实例(学生资料表中的新行)

I am trying to create user, where User model has two User profiles, e.g. Teacher and Student. At the time of creating user, I want to create User instance in Teacher or Student profile also.

Here's my Serializer classes:

class StudentSerializer(serializers.ModelSerializer): class Meta: model = Student fields = ('id', 'user_id') class TeacherSerializer(serializers.ModelSerializer): class Meta: model = Teacher fields = ('id', 'user_id') class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ( 'id', 'username', 'password', 'email', 'user_type' ) extra_kwargs = { 'password': {'write_only': True}, } def create(self, validated_data): user = User.objects.create_user(**validated_data) return user

Here's the custom register view:

class CreateUserView(CreateAPIView): model = User permission_classes = [ permissions.AllowAny # Or anon users can't register ] serializer_class = UserSerializer

Right now, I am able to create User and but creating user profile according to the user_type parameter is a challenge for me.

So, if user_type is student, I need to create User instance for the StudentProfile as well(a new row in student profile table)

最满意答案

也许你可以添加简单的条件?

def create(self, validated_data): user = User.objects.create_user(**validated_data) if validated_data('user_type') == 'student': Student.objects.create(user_id=user.id) else: Teacher.objects.create(user_id=user.id) return user

May be you can add simple if condition?:

def create(self, validated_data): user = User.objects.create_user(**validated_data) if validated_data('user_type') == 'student': Student.objects.create(user_id=user.id) else: Teacher.objects.create(user_id=user.id) return user

更多推荐

本文发布于:2023-07-23 02:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1226496.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:配置文件   模型   两个   用户   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!