1
看到重複的鍵錯誤我有一個用戶配置類:更新與/嵌套資源(用戶/用戶配置文件) - 上PATCH
class UserProfile(models.Model):
user = models.OneToOneField(User)
bio = models.CharField(max_length=180, null=True)
鏈接到User類。
AUTH_PROFILE_MODULE = 'plantvillage.userprofile'
我想通過tastypie提供一個API,讓用戶的細節和USERPROFILE細節可以在同一時間進行管理。如果可能,我想而不是公開兩個接口(一個用於用戶,另一個用於userprofiles)。
設置我的資源,像這樣:
class ProfileResource(ModelResource):
class Meta:
queryset = UserProfile.objects.all()
resource_name = 'profile'
authentication = ApiKeyAuthentication()
authorization = DjangoAuthorization()
allowed_methods = ['get', 'put', 'patch']
class UserResource(ModelResource):
profile = fields.ToOneField(ProfileResource, 'userprofile', full=True)
class Meta:
queryset = User.objects.filter(is_staff=False)
resource_name = 'usr'
authentication = ApiKeyAuthentication()
authorization = DjangoAuthorization()
excludes = ['password', 'is_active', 'is_staff']
不過,更新這個錯誤
curl --dump-header - -H "Authorization: ApiKey [email protected]:1432ece6a1f34fae24a77315b5c924f756f13807" -H "Content-Type: application/json" -X PATCH --data '{"profile":{"bio":"aquarium"}}' "http://127.0.0.1:8000/api/usr/25/"
結果:
"error_message": "duplicate key value violates unique constraint \"plantvillage_userprofile_user_id_key\"\n", "traceback": "Traceback (most recent call last):\n\n File \"/usr/local/lib/python2.6/dist-packages/django_tastypie-0.9.12_alpha-py2.6.egg/tastypie/resources.py\", line 196, in wrapper\n
我可以做什麼樣的變化,使這項工作?