2012-05-15 35 views
6

Tôi đang tạo UserResource và UserProfileResource. Khi tôi tạo một người dùng mới, tôi thấy rằng bảng cơ sở dữ liệu userprofile của tôi cũng đang được cập nhật. Cái nào tốt. Nhưng khi tôi GET người sử dụng tôi nhận được một lỗi nói:Phím tắt Tastypie được đặt thành rỗng

The model '' has an empty attribute 'profile' and doesn't allow a null value. 

Mã của tôi:

resources.py

class UserProfileResource(ModelResource): 
    home_address = fields.CharField(attribute='home_address') 
    user = fields.ToManyField('resources.UserResource', attribute='user', related_name='profile') 
    class Meta: 
     queryset = UserProfile.objects.all() 
     resource_name = 'profile' 
     allowed_methods = ['get', 'post', 'delete', 'put'] 
     fields = ['home_address'] 
     authorization = Authorization() 
     include_resource_uri = False 
     include_absolute_url = False 

class UserResource(ModelResource): 
    profile = fields.ToOneField('resources.UserProfileResource', attribute = 'profile', related_name='user', full=True) 
    class Meta: 
     queryset = User.objects.all() 
     resource_name = 'user' 
     allowed_methods = ['get', 'post', 'delete', 'put'] 
     fields = ['username'] 
     filtering = { 
       'username': ALL, 
       } 
     authorization = Authorization() 

    def obj_create(self, bundle, request=None, **kwargs): 
     try: 
      bundle = super(UserResource, self).obj_create(bundle, request, **kwargs) 
      bundle.obj.set_password(bundle.data.get('password')) 
      bundle.obj.save() 
     except IntegrityError: 
      raise BadRequest('IntegrityError') 
     return bundle 

models.py

class UserProfile(models.Model): 
    user = models.ForeignKey(User, unique=True) 
    home_address = models.TextField() 

Bất cứ ý tưởng nào? johnoc

Trả lời

15

Sau rất nhiều tìm kiếm, tôi cuối cùng đã có câu trả lời cho điều này.

models.py

class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    home_address = models.TextField() 

resources.py

class UserProfileResource(ModelResource): 
    class Meta: 
     queryset = UserProfile.objects.all() 
     resource_name = 'profile' 
     allowed_methods = ['get', 'post', 'delete', 'put'] 
     fields = ['home_address'] 
     authorization = Authorization() 
     include_resource_uri = False 
     include_absolute_url = False 

class UserResource(ModelResource): 
    profile = fields.ToOneField('resources.UserProfileResource', attribute = 'userprofile', related_name='user', full=True, null=True) 
    class Meta: 
     queryset = User.objects.all() 
     resource_name = 'user' 
     allowed_methods = ['get', 'post', 'delete', 'put'] 
     fields = ['username'] 
     filtering = { 
       'username': ALL, 
       } 
     authorization = Authorization() 

    def obj_create(self, bundle, request=None, **kwargs): 
     try: 
      bundle = super(UserResource, self).obj_create(bundle, request, **kwargs) 
      bundle.obj.set_password(bundle.data.get('password')) 
      bundle.obj.save() 
     except IntegrityError: 
      raise BadRequest('IntegrityError') 
     return bundle 

này hoạt động. Lưu ý rằng trường mô hình đã thay đổi thành OneToOneField và thuộc tính tài nguyên giờ là "userprofile". Tôi hy vọng điều này sẽ giúp ích cho ai đó!

John

+0

Cảm ơn! Đã lưu một vài giờ cho tôi. :) Upvoted. –

+0

@ johnoc, cảm ơn vì điều này, nó đã giúp tôi rất nhiều! – noahandthewhale

+0

Làm tốt .... giữ nó lên. :) – CrazyGeek