使用ajax更新tastypie资源

时间:2014-02-26 18:50:46

标签: jquery python ajax django tastypie

所以,我有我的用户模型:

class User(AbstractBaseUser, PermissionsMixin):
    ...
    follows = models.ManyToManyField('self', related_name='followers', symmetrical=False)

和我的UserResource:

class UserResource(ModelResource):
    followers = fields.ToManyField('self', 'followers', full=True)

    class Meta:
        queryset = User.objects.all()
        resource_name = 'user'
        fields = ['username', 'email', 'followers']

        authentication = SessionAuthentication()
        authorization = DjangoAuthorization()

我从/ api / v1 / user / 1的API获得此响应:

{"email": "root@root.com", "followers": [{"email": "asf@afa.com", "followers": [], "resource_uri": "", "username": "123"}], "resource_uri": "", "username": "root@root.com"}

让我假装我想将这个关注者添加到用户:

var follower = JSON.stringify({
    {"email": "new@mail.com", "followers": [], "resource_uri": "", "username": "new"}
});

如何创建我的AJAX请求?感谢任何帮助,我被困住了。

0 个答案:

没有答案