2017-01-05 3 views
0

これはエラー "error_message"を表示しています: "基数10のint()の無効なリテラル: ''" ユーザリクエストに対してこのエラーが発生します。私のユーザモデルはdjango tastypie patchは私に "500"のエラーを返します

class User(AbstractBaseUser): 

    VIA_FACEBOOK = 1 
    VIA_GOOGLE = 2 

    SIGN_IN_TYPES = (
     (VIA_FACEBOOK,'facebook'), 
     (VIA_GOOGLE,'google') 
    ) 

    MALE = 1 
    FEMALE = 0 

    GENDERA_TYPES = (
     (FEMALE,'female'), 
     (MALE,'male') 
     ) 

    SINGLE_ACCOUNT_USER = 0 
    JOINT_ACCOUNT_USER = 1 


    USER_TYPES = (
     (SINGLE_ACCOUNT_USER,'single_accout_user'), 
     (JOINT_ACCOUNT_USER,'joint_accout_user') 

    ) 
    first_name = models.CharField(blank = True, max_length=50) 
    middle_name = models.CharField(blank=True, max_length=50,null=True) 
    last_name = models.CharField(blank=True, max_length=50,null=True) 
    email = models.EmailField(blank=False,unique=True,max_length=100) 
    sign_in_via = models.SmallIntegerField(choices=SIGN_IN_TYPES, default=0) 
    mobile = models.CharField(blank=True, max_length=10,null=True) 
    dob = models.DateField(blank=True, null=True) 
    profile_pic_url = models.CharField(blank=True,max_length=300,null=True) 
    background_pic_url = models.CharField(blank=True,max_length=300,null=True) 
    gender = models.SmallIntegerField(blank=True,choices=GENDERA_TYPES,null=True) 
    user_type = models.SmallIntegerField(blank=False,choices=USER_TYPES,default=0) 
    details = models.CharField(blank=True,max_length=500,null=True) 
    created_on = models.DateTimeField(auto_now=True) 
    slug = models.SlugField(default='') 

    USERNAME_FIELD = 'email' 

MYユーザーのリソーススクリプトは次のとおりです。

class UserResource(ModelResource): 
    class Meta: 
     queryset = User.objects.all() 
     resource_name = 'user' 
     list_allowed_methods = ['get', 'post', 'patch', 'put','delete'] 
     authorization = Authorization() 
     include_resource_uri = True 
     always_return_data = True 
     authentication = Authentication() 
     filtering = { 
      'id': ALL, 
      'email': ALL, 
      'user_type': ALL, 
      'mobile': ALL, 
      'created_at': ALL, 
     } 
     excludes = ['created_on','password','last_login','sign_in_via'] 

GETとPOSTが正常に動作しているが、PATCHとPUTメソッドが機能していません。 私はポストマン を使用して要求を作っています、PATCHリクエスト(http://127.0.0.1:8888/api/user_api/user/14/?format=json)のための身体は、私は500、内部サーバーエラーを取得しています

{ 
    "background_pic_url": "h/h", 
    "last_name":"sdfsd" 
} 

になります。スタックトレースが

{ 
    "error_message": "invalid literal for int() with base 10: ''", 
    "traceback": "Traceback (most recent call last): 

    File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 220, in wrapper 
    response = callback(request, *args, **kwargs) 

    File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 460, in dispatch_detail 
    return self.dispatch('detail', request, **kwargs) 

    File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 483, in dispatch 
    response = method(request, **kwargs) 

    File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 1669, in patch_detail 
    bundle = self.full_dehydrate(bundle) 

    File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 885, in full_dehydrate 
    data[field_name] = field_object.dehydrate(bundle, for_list=for_list) 

    File \"C:\\Python27\\lib\\site-packages\\tastypie\\fields.py\", line 146, in dehydrate 
    return self.convert(current_object) 

    File \"C:\\Python27\\lib\\site-packages\\tastypie\\fields.py\", line 255, in convert 
    return int(value)\n\nValueError: invalid literal for int() with base 10: ''" 

} 

およびPUTリクエスト用で、体はまた、私は500内部サーバーエラーを取得しています。このため

{ 
    "email":"[email protected]", 
    "first_name":"a", 
    "middle_name":"b", 
    "last_name":"c", 
    "mobile":"1234567890", 
    "details":"nothing", 
    "profile_pic_url":"n/n", 
    "background_pic_url":"v/v", 
    "sign_in_via":"1", 
    "dob":"2017-01-29", 
    "user_type":"1", 
    "gender":"2" 
} 

になります。スタックトレースは:

{ 
    "error_message": "'dict' object has no attribute 'first_name'", 
    "traceback": "Traceback (most recent call last): 

    File  \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 220, in wrapper 
    response = callback(request, *args, **kwargs) 

    File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 460, in dispatch_detail 
    return self.dispatch('detail', request, **kwargs) 

    File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 483, in dispatch 
    response = method(request, **kwargs) 

    File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 1472, in put_detail 
    updated_bundle = self.obj_update(bundle=bundle, **self.remove_api_resource_names(kwargs)) 

    File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 2226, in obj_update 
    bundle = self.full_hydrate(bundle) 

    File \"C:\\Python27\\lib\\site-packages\\tastypie\\resources.py\", line 939, in full_hydrate 
    setattr(bundle.obj, field_object.attribute, value)\n\nAttributeError: 'dict' object has no attribute 'first_name'" 

} 

私はこれらのエラーを取得する手がかりがありません。

+0

私はtastypie、PUT、PATCH、GET、POSTを使用しているのでコードが必要ないので、requsest – Juggernaut

+0

ちょっと@AminEtesamianを扱っているコントローラのコードを見せてください。リソースはこれらの要求を自動的に処理します。これらの要求に対してヘルパーメソッドを使用していません。 – hemanth5636

答えて

1

あなたのPUT本体があります

{ 
    ... 
    "user_type": "1", 
    "gender": "2" 
} 

とあなたのモデルフィールド:

gender = models.SmallIntegerField(blank=True,choices=GENDERA_TYPES,null=True) 
user_type = models.SmallIntegerField(blank=False,choices=USER_TYPES,default=0) 

ので、あなたもモデル

{ 
    ... 
    "user_type": 1, 
    "gender": 2 
} 
に指定されたあなたのような数字とPUT/PATCHリクエストを行う必要があります

はJSONで有効です。

これが役に立ちます。

よろしくお願いいたします。

関連する問題