【问题标题】:Tastypie foreignkey set to nullTastypie 外键设置为空
【发布时间】:2012-05-15 14:49:02
【问题描述】:

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

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

有人有什么想法吗? 约翰克

【问题讨论】:

    标签: django tastypie


    【解决方案1】:

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

    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,并且资源属性现在为“userprofile”。我希望这可以帮助那里的人!

    约翰

    【讨论】:

      猜你喜欢
      • 2019-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 2014-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多