Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.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 将照片上载到Django Rest框架_Python_Django_Rest_Django Rest Framework - Fatal编程技术网

Python 将照片上载到Django Rest框架

Python 将照片上载到Django Rest框架,python,django,rest,django-rest-framework,Python,Django,Rest,Django Rest Framework,我有我的模型和诸如此类的东西,当我试图上传图像时,我得到的是: 'PhotoSerializer' object has no attribute '_committed 我的看法是: class UploadPhoto(APIView): #authentication_classes = (TokenAuthentication,) permission_classes = ()#(IsAuthenticated,) def put(self, request,

我有我的模型和诸如此类的东西,当我试图上传图像时,我得到的是:

'PhotoSerializer' object has no attribute '_committed
我的看法是:

class UploadPhoto(APIView):
    #authentication_classes = (TokenAuthentication,)
    permission_classes = ()#(IsAuthenticated,)

    def put(self, request, username):

        user = User.objects.get(username = username)
        userprofile = UserProfile.objects.get(user= user)
        photo = PhotoSerializer(data = request.data)
        userprofile.photo = photo
        userprofile.save()
        return Response(status= status.HTTP_200_OK)
序列化程序:

class PhotoSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile
        fields = (
            'photo',
        )
至少是我的模特

class UserProfile(models.Model):
    """
    User Profile

    """
    user = models.OneToOneField(User, related_name='userprofile')

    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default='M')

    phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$',message="Phone must be entered in the format: '+999999999'. Up 15 digits allowed.")
    #The Field on DataBase after check if it's a valid Phone Number.
    # validators should be a list
    phone_number = models.CharField(validators=[phone_regex], max_length=15, blank=True) 
    photo = models.ImageField(upload_to = 'photos/', null = True)

    driver_passenger = models.BooleanField(default=True)
    rides_given = models.IntegerField(default=0) 
    rides_taken = models.IntegerField(default=0)
    reputation = models.IntegerField(default=0)

我尝试了一些上传照片的选项,但我无法实现,因此我感谢您的帮助(:

好吧,在尝试了一点之后,我可以用这个上传图像 代码希望是有用的

View.py

class UploadPhoto(APIView):
    #authentication_classes = (TokenAuthentication,)
    permission_classes = ()#(IsAuthenticated,)

    def put(self, request, username):

        user = User.objects.get(username = username)
        userprofile = UserProfile.objects.get(user= user)
        photo = PhotoSerializer(userprofile,data = request.data)
        photo.is_valid(raise_exception=True)
        photo.save()

        return Response(status= status.HTTP_200_OK)