Tastypie foreignkey设置为null

时间:2012-05-15 14:49:03

标签: django tastypie

我正在创建UserResource和UserProfileResource。当我创建一个新用户时,我看到我的userprofile数据库表也在被更新。这很好。但当我获取用户时,我收到一条错误消息:

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

我的代码:

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()

任何想法? johnoc

1 个答案:

答案 0 :(得分:15)

经过大量的搜索,我终于得到了答案。

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

这很有效。请注意,模型字段已更改为OneToOneField,而resources属性现在为“userprofile”。我希望这可以帮助那里的人!

约翰