我有这个模型:
class UserSub(models.Model):
user = models.OneToOneField(User, related_name='userSub')
amigos = models.ManyToManyField('self', null=True)
title = models.TextField()
导入的 Django 用户模型。
以及以下资源:
class UserResource(ModelResource):
usersub = fields.OneToOneField('test.api.UserSubResource', attribute = 'personal', related_name='user', full=True, null=True)
class Meta:
object_class = User
fields = ['username', 'first_name', 'last_name', 'password', 'email']
detail_allowed_methods = ['get', 'post', 'put']
authentication = Authentication()
authorization = Authorization()
queryset = User.objects.all()
resource_name = 'users'
excludes = ['id']
class UserSubResource(ModelResource):
user = fields.OneToOneField('test.api.UserResource', attribute = 'user', related_name = 'userSub')
amigos= fields.ToManyField('test.api.UserSubResource', attribute = 'amigos', null=True)
class Meta:
object_class = UserSub
fields = ['title']
detail_allowed_methods = ['get', 'post', 'put', 'patch']
authentication = Authentication()
authorization = Authorization()
always_return_data = True
queryset = UserSub.objects.all()
resource_name = 'usersub'
excludes = ['id']
我正在尝试更新特定用户的朋友值。我的数据是:
usersub_json: {"amigos":["/api/v1/usersub/9/","/api/v1/usersub/8/"]}
$.ajax({
url : 'http://127.0.0.1:8000' + usersub_uri,
type : 'PUT',
contentType : 'application/json',
data : usersub_json,
dataType : 'json',
processData : false,
error : function(http) {
if (http.responseText != "") {
alert(http.responseText);
}
}
})
我从 PUT 请求中得到“ 202 ACCEPTED ”,并且朋友没有更新。
并且来自 PATCH 请求的“ 202 ACCEPTED ”,并且朋友没有更新。
如果我在创建 usersub 时在第一个发布请求中添加朋友,它会成功地将它们添加到数据库中。但是如果我使用 PUT 或 PATCH 向数组添加更多内容,则不会更新。