创建具有外键用户的资源

时间:2014-04-12 13:08:31

标签: django tastypie

我有一个与用户相关的模型。我已经为模型定义了一个资源。我无法通过邮件创建新资源。我刚刚得到{"错误":""我发帖时回来: {" user":{id:1}," status":" Checkin to home。"} 要么 {" user_id":1," status":" Checkin to home。"}

代码如下。任何建议都将受到高度赞赏。

型号:

class Checkin(models.Model):
    user = models.ForeignKey(User)
    status = models.TextField()

    def __unicode__(self):
        return self.status

资源:

class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        resource_name = 'user'
        excludes = ['password', 'is_active', 'is_staff', 'is_superuser']
        allowed_methods = ['get']

class CheckinResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user')
    class Meta:
        queryset = Checkin.objects.all()
        resource_name = 'checkin'
        filtering = { "status" : ALL }
        authorization = Authorization()

1 个答案:

答案 0 :(得分:0)

发送的json应包含资源uri

例如     {"用户":" / djangoapp / api / v1 / user / 1 /","状态":"再次登记回家。&# 34;}

如果您想要返回创建的资源,还需要设置always_return_data = True

例如

class CheckinResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user')
    class Meta:
        queryset = Checkin.objects.all()
        resource_name = 'checkin'
        filtering = { "status" : ALL }
        always_return_data = True
        authorization = Authorization()