我有这样的模式:
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接受 ”,而不是更新的吾友。
而“202接受 ”从PATCH请求,戈斯不作更新。
如果我创建一个USERSUB当在第一篇文章的请求添加吾友,这将它们添加到数据库中成功。 但是,如果我增加更多的使用PUT或PATCH阵列未更新。