【问题标题】:Nested UserProfile in User resource django tastypie用户资源 django tastepie 中的嵌套 UserProfile
【发布时间】:2012-07-09 11:46:31
【问题描述】:

我正在使用 django sweetpie,我正在尝试在我的用户资源中添加一个嵌套的用户配置文件资源:

我的代码:

模型(在 APP_FOLDER/models.py 中):

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='userprofile')

资源定义(在 API/resources.py 中):

class UserResource(ModelResource):
    userprofile = fields.ToManyField('api.resources.UserProfileResource', 'userprofile', full=True)

    class Meta:
        queryset = User.objects.all()
        resource_name = 'user'

class UserProfileResource(ModelResource):
    user = fields.ToOneField(UserResource,'user')

    class Meta:
        queryset = UserProfile.objects.all()
        resource_name = 'userprofile'

当我尝试访问用户时,我得到:error_message:“'UserProfile' 对象没有属性 'all'”。我错过了什么吗?

【问题讨论】:

    标签: django api tastypie


    【解决方案1】:

    我想通了:我需要在 UserResource 中将 fields.ToManyField 更改为 fields.ToOneField,因为模型中的关系是一对一的。

    【讨论】:

      猜你喜欢
      • 2012-11-02
      • 2014-12-15
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      相关资源
      最近更新 更多