2012-08-06 2 views
1

에 중복 키 오류를보고는 내가 USERPROFILE 클래스가 : 사용자 클래스에 연결되어업데이트 관련/중첩 된 자원 (사용자/사용자 프로필) PATCH

class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    bio = models.CharField(max_length=180, null=True) 

.

AUTH_PROFILE_MODULE = 'plantvillage.userprofile' 

사용자 정보와 사용자 프로필 세부 정보를 동시에 관리 할 수 ​​있도록 tastypie를 통해 하나의 API를 제공하고 싶습니다. 이 아니라면은 가능하면 두 개의 인터페이스 (하나는 사용자 용, 하나는 userprofiles 용)를 노출하고 싶습니다.

나는 그렇게처럼 내 자원을 설정 :이 오류에

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/" 

결과를 업데이트 그러나

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'] 

, :이 작업을 할 수 있습니다 어떤 변화

"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 

?

답변

2
당신은 완전히 UserResource 제거하고이 같은 노출 할 User 모델에서 필드를 추가 얻을 수있는

:이 내용은 GET 요청의 경우에 User 모델에서 적절한 데이터를 전송하지 않습니다

username = fields.CharField(attribute = 'user__username') 

하지만, 또한 업데이 트를 돌봐.