字段未显示drf django

时间:2020-01-02 06:54:44

标签: python django django-rest-framework

我正在尝试从drf注册用户并创建个人资料:

models.py:

class UserManager(BaseUserManager):
    def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
        if not email:
            raise ValueError('user must have email address')
            now = timezone.now()
            email = self.normalize_email(email)
            user = self.model(
            email=email,
            is_staff=is_staff,
            is_active=True,
            is_superuser=is_superuser,
            last_login=now,
            date_joined=now,
            **extra_fields
            )
        if password:
            user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        return self._create_user(email, password, False, False, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        user=self._create_user(email, password, True, True, **extra_fields)
        user.save(using=self._db)
        return user

class CustomUser(AbstractBaseUser, PermissionsMixin):
    USER_TYPE_CHOICES = (
        (1, 'Freelance Photographer'),
        (2, 'photographer'),
        (3, 'client'),
    )
    user_type = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES, null=True)
    email = models.EmailField(max_length = 100, unique = True)
    First_Name = models.CharField(max_length = 100, null = True, blank = True)
    Last_Name = models.CharField(max_length = 100, null = True, blank = True)
    is_client = models.BooleanField(default=False)
    is_photographer = models.BooleanField(default=False)
    is_staff = models.BooleanField(default = False)
    is_superuser = models.BooleanField(default = False)
    is_active = models.BooleanField(default = True)
    last_login = models.DateTimeField(null = True, blank = True)
    date_joined = models.DateTimeField(auto_now_add=True)

    USERNAME_FIELD = "email"
    EMAIL_FIELD = "email"
    REQUIRED_FIELD = []

    objects = UserManager()

    def get_absolute_url(self):
        return "/users/%i/" % (self.pk)

class UserProfile(models.Model):
    user = models.OneToOneField(CustomUser,related_name='profile', on_delete=models.CASCADE, null=True)

    Image = models.ImageField(upload_to='profile',default='profile/demo.jpg')
    Mobile_Number = models.CharField(max_length=20, null=True, blank=True)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

serializers.py:

class UserProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile
        fields = ('image','mobile_number','created_at','updated_at')



class UserSerializer(serializers.HyperlinkedModelSerializer):
    profile = UserProfileSerializer(required=False)

    class Meta:
        model = UserModel
        fields = ( "id", "email", "password","First_Name","Last_Name","profile")

    def create(self, validated_data):
        profile_data = validated_data.pop('profile')
        user = UserModel.objects.create(**validated_data)        
        user.set_password(validated_data['password'])
        user.save()
        UserProfile.objects.create(user=user,**profile_data)
        to_mail = user.email
        subject = 'Welcome to Liveimages'
        message = 'Welcome to Liveimages Community'
        send_mail(subject,message,'nandanc465@gmail.com',[to_mail,],fail_silently=False,)
        return user

    def update(self,instance, validated_data):
        profile_data = validated_data.pop('profile')
        profile = instance.profile
        instance.save()

        profile.image = profile_data.get('image', profile.image)
        profile.mobile_number = profile_data.get('mobile_number', profile.mobile_number)
        profile.save()
        return instance

views.py:

class UserViewSet(generics.ListCreateAPIView):
    authentication_classes = []
    permission_classes = [] 
    queryset = CustomUser.objects.all()       
    serializer_class = UserSerializer  

urls.py:

path('user/', views.UserViewSet.as_view()),

当我访问localhost:8000 / api / user /时,它仅显示密码字段,我不明白为什么它应该显示用于注册的所有字段

enter image description here

............................................... ................................................... ................................................... ...........................

0 个答案:

没有答案