2015-12-16 54 views
0

我想上傳一個編碼的BASE64圖像,當我註冊一個新的用戶爲我的應用程序我在我的用戶配置文件內使用一個ImageField 問題是API給我以下錯誤。上傳圖像上json DJANGO休息框架

{ 
    "userprofile": { 
    "photo": [ 
     "The submitted data was not a file. Check the encoding type on the form." 
    ] 
    } 
} 

這是我在我用報頭的Content-Type POST請求的請求信息:應用/ JSON用於請求我使用郵差從谷歌

{ 

    "username": "is690002", 
    "password": "is690002", 
    "first_name": "andres ", 
    "last_name": "Barragan", 
    "email": "[email protected]", 
    "userprofile": { 
     "gender": "F", 
     "phone_number": "3315854644", 
     "photo": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYAB......." 
    } 
    } 

這是我的model.py

class UserProfile(models.Model): 
    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 = 'C:\ProjectsDJ\carpoolapp\photos', null = True) 

我的串行

class UserProfileSerializer(serializers.ModelSerializer): 
    photo = serializers.ImageField(max_length=None, use_url=False) 
    class Meta: 
     model = UserProfile 
     fields = (
      'gender', 
      'phone_number', 
      'photo' 
      ) 


class UserRegistrationSerializer(serializers.HyperlinkedModelSerializer): 
    userprofile = UserProfileSerializer() 
    class Meta: 
     model = User 
     fields = (
      'username', 
      'first_name', 
      'last_name', 
      'email', 
      'password', 
      'userprofile' 
      ) 
     extra_kwargs = {'password': {'write_only': True}} 

#@Override create for create a user and profile from HTTP Request 
    def create(self, validated_data): # 
      userprofile_data = validated_data.pop('userprofile') 
      user = User.objects.create(**validated_data) # Create the user object instance before store in the DB 
      user.set_password(validated_data['password']) #Hash to the Password of the user instance 
      user.save() #Save the Hashed Password 
      UserProfile.objects.create(user=user, **userprofile_data) 
      return user #Return user object 

我View.py

class UserRegister(APIView): 

    permission_classes =() 
    def post(self, request): 
     serializer = UserRegistrationSerializer(data = request.data) #, files = request.FILES) 
     serializer.is_valid(raise_exception=True) # If the JSON Is 
     serializer.save() #Save user in DB 
     return Response(status=status.HTTP_201_CREATED) 

注:一切都很好,如果我不使用的ImageField我可以創建用戶等,還我想這裏有兩個教程的StackOverflow

+0

入住github上的DRF測試。這可能會幫助你。 https://github.com/tomchristie/django-rest-framework/blob/6284bceaaff0e53349131164ce5c16cda8deb715/tests/test_testing.py#L233-L238 –

回答

3

Django的REST框架不通過JSON支持開箱即用的文件上傳。 The documentation mentions that。也許第三方呢。

請注意,DRF中的文件上載測試使用表單內容類型而不是JSON。

0

用於註冊用戶RegisterSerializer

class RegistrationSerializer(RegisterSerializer): 

""" 
Formulario de registro personalizado 
""" 
first_name = serializers.CharField(max_length=50) 
last_name = serializers.CharField(max_length=50) 
phone_number = serializers.CharField(max_length=30) 
street_name = serializers.CharField(max_length=50) 
int_number = serializers.CharField(max_length=5) 
ext_number = serializers.CharField(max_length=5) 
suburb = serializers.CharField(max_length=60) 
city = serializers.CharField(max_length=60) 
cp = serializers.CharField(max_length=50) 
state = serializers.CharField(max_length=60) 
backup_phone = serializers.CharField(max_length=30, allow_blank=True) 
curp = serializers.CharField(max_length=50) 

avatar = serializers.ImageField(allow_empty_file=True, required=False) 

def get_cleaned_data(self): 
    pass 

def save(self, request): 

    pass 

它爲我工作。

在郵遞員

enter image description here