Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 未显示drf django的字段_Python_Django_Django Rest Framework - Fatal编程技术网

Python 未显示drf django的字段

Python 未显示drf django的字段,python,django,django-rest-framework,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') n

我正在尝试从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  
URL.py:

path('user/', views.UserViewSet.as_view()),
当我访问localhost:8000/api/user/时,它只显示密码字段,我不明白为什么它应该显示注册的所有字段


尝试将
类UserSerializer(serializers.HyperlinkedModelSerializer):
更改为
类UserSerializer(serializers.ModelSerializer):
它仍然不工作class Meta:model=UserModel这不应该是class Meta:model=CustomUseri我发现有相同的名字serializer什么是
UserModel
?!尝试将
类UserSerializer(serializers.HyperlinkedModelSerializer):
更改为
类UserSerializer(serializers.ModelSerializer):
它仍然不工作class Meta:model=UserModel这不应该是class Meta:model=CustomUseri我发现有相同的名字serializer什么是
UserModel
?!