[英]The right way to use Ajax to Update a Single field in a specific Object
Is it possible to use Ajax to Update a Single field in a Specific Object? 是否可以使用Ajax更新特定对象中的单个字段? I have an postgres table with lots of records, I want to use a jquery Ajax request to update a single field in a specific object within that table.
我有一个包含大量记录的postgres表,我想使用一个jQuery Ajax请求来更新该表中特定对象中的单个字段。 Can that be done without replacing or reposting the entire record?.
可以在不替换或重新发布整个记录的情况下完成此操作吗?
I want this (Gives me a 400 bad request error): 我想要这个(给我一个400错误的请求错误):
$.ajax({
type: "POST",
url: '/api/MyEndPoint/',
data: {
id: Specific_Record,
Field_To_Update: New_Value,
},
success: function(data){
console.log( 'success, server says '+data);
}
});
Instead of this (which works): 代替这个(有效):
$.ajax({
type: "POST",
url: '/api/MyEndPoint/',
data: {
id: Specific_Record,
Field_To_Update: New_Value,
Field1: SameAsBefore,
Field2: SameAsBefore,
Field3: SameAsBefore,
...
Field16: SameAsBefore,
},
success: function(data){
console.log( 'success, server says '+data);
}
});
*Note: I'm using Django, and could easily do this update in views.py but I want to use Javascript to avoid a page refresh. *注意:我使用的是Django,可以在views.py中轻松进行此更新,但是我想使用Javascript来避免页面刷新。
Since I'm also using Django Rest Framework, would it be better for me to create a new endpoint that is specific to the field I want to update? 由于我也在使用Django Rest Framework,因此创建一个特定于我要更新的字段的新端点会更好吗? ex:
/api/DB_Table/Object_id/Field_to_Update
例如:
/api/DB_Table/Object_id/Field_to_Update
Thanks! 谢谢!
Yes, you can do this using the PATCH HTTP method and it is called a partial update in REST terminology. 是的,您可以使用PATCH HTTP方法执行此操作,在REST术语中将其称为部分更新 。 It is already implemented by default in DRF so you don't have to override any method for that.
默认情况下,它已在DRF中实现,因此您不必重写任何方法。 This is opposed to PUT which is a full update and will require that you add all required fields to the request unless yuou override the default behavior
这与PUT是完全更新相反,PUT要求您将所有必填字段添加到请求中,除非yuou覆盖默认行为
I think you can in simplest form use the RetrieveUpdateAPIView
generic view provided by Django RestFramework library 我认为您可以以最简单的形式使用Django RestFramework库提供的
RetrieveUpdateAPIView
通用视图
This will expose following apis : 这将公开以下api:
GET
: Return
object GET
: Return
对象
PATCH
: Update
the object data ( partially
, as per convention) PATCH
: Update
对象数据(按惯例partially
Update
)
PUT
: Update
the object data PUT
: Update
对象数据
Reference : https://www.django-rest-framework.org/api-guide/generic-views/#retrieveupdateapiview 参考: https : //www.django-rest-framework.org/api-guide/generic-views/#retrieveupdateapiview
If you also want to support deleting
object, then you can use RetrieveUpdateDestroyAPIView
, this will support DELETE
to delete object 如果您还希望
support deleting
对象,则可以使用RetrieveUpdateDestroyAPIView
,这将支持DELETE
删除对象
Reference : https://www.django-rest-framework.org/api-guide/generic-views/#retrieveupdatedestroyapiview 参考: https : //www.django-rest-framework.org/api-guide/generic-views/#retrieveupdatedestroyapiview
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.