[Django Rest框架]:如何在注册页面添加phone_number字段?
在Django Rest框架中,要在注册页面添加phone_number字段,可以按照以下步骤进行操作:
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
phone_number = models.CharField(max_length=20, blank=True, null=True)
from rest_framework import serializers
from .models import User
class UserSerializer(serializers.ModelSerializer):
phone_number = serializers.CharField(required=False)
class Meta:
model = User
fields = ['username', 'email', 'phone_number', 'password']
from rest_framework import generics
from .models import User
from .serializers import UserSerializer
class UserCreateView(generics.CreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
from django.urls import path
from .views import UserCreateView
urlpatterns = [
path('api/register/', UserCreateView.as_view(), name='register'),
]
现在,你的注册页面就已经添加了phone_number字段。用户在注册时可以填写电话号码,并且该字段会被保存到数据库中。
推荐的腾讯云相关产品:腾讯云云服务器(CVM)和腾讯云数据库MySQL。
领取专属 10元无门槛券
手把手带您无忧上云