Repository navigation
refactor: update BatchDeleteInstance data handling in ChatUserSerializer - #8213
Conversation
--bug=1080671@tapd-45245355 --user=王孝刚 [AI接口测试] v3 批量删除对话用户接口恒 500(serializer 无 data 即调用 is_valid) https://www.tapd.cn/45245355/s/2178261
| return result.success(ChatUserSerializer.BatchDeleteInstance(data={"ids": request.data}).batch_delete()) | ||
|
|
||
| class BatchAddGroup(APIView): | ||
| authentication_classes = [TokenAuth] |
There was a problem hiding this comment.
The changed line is likely a correct DRF fix, but there is a more serious data-shape issue nearby.
1. data= keyword change
If ChatUserSerializer.BatchDeleteInstance is a DRF Serializer subclass, this change is correct:
BatchDeleteInstance(data={...})In DRF, the first positional argument is instance, not data:
Serializer(instance=None, data=empty, **kwargs)So the original:
BatchDeleteInstance({"ids": request.data})would pass the dict as instance, not as input data, which is wrong for validation/serialization.
Caveat: If BatchDeleteInstance is a custom class, confirm its constructor actually accepts data=. If its signature is something like __init__(self, payload), the keyword change will break.
2. Main issue: request.data is likely nested incorrectly
This part is suspicious:
data={"ids": request.data}If the client sends:
{"ids": [1, 2, 3]}then request.data is:
{"ids": [1, 2, 3]}and the serializer receives:
{"ids": {"ids": [1, 2, 3]}}So ids becomes a dict instead of a list. That is almost certainly a bug.
It is also inconsistent with the operation-object lambda above:
get_operation_object=lambda r, k: get_batch_delete_user_operation_object(r.data.get("ids", [])),which correctly extracts request.data.get("ids", []).
Suggested correction
def post(self, request: Request):
ids = request.data.get("ids", [])
serializer = ChatUserSerializer.BatchDeleteInstance(data={"ids": ids})
if hasattr(serializer, "is_valid"):
serializer.is_valid(raise_exception=True)
return result.success(serializer.batch_delete())If request.data can also be a raw list, handle that explicitly, but prefer a consistent payload shape:
{"ids": [1, 2, 3]}3. Additional potential issues
- Empty
ids: Ensureids=[]does not accidentally mean “delete all”. Reject empty lists. - Validation: Verify
idsis a list of integers, not a string, dict, or arbitrary objects. - Batch size limit: Add a maximum number of IDs per request to avoid abuse or long-running transactions.
- Authorization: Check that the requesting user is allowed to delete every ID, not just the first one.
- Transactions: Wrap the batch delete in a transaction if partial deletion would be inconsistent.
- Bulk operations: If
batch_delete()loops and deletes one by one, optimize withfilter(pk__in=ids).delete()or a bulk update/soft-delete. - Audit consistency: Use the same validated
idslist for the operation object and the deletion logic. QueryDicthandling: If form-encoded requests are supported,request.data.get("ids")may not return a list. Usegetlist("ids")or require JSON.
Verdict
The data= change is probably correct for DRF. However, the bigger issue is that {"ids": request.data} likely nests the whole request body under ids. Use request.data.get("ids", []) instead, and validate/authorize the IDs before deleting.
refactor: update BatchDeleteInstance data handling in ChatUserSerializer --bug=1080671@tapd-45245355 --user=王孝刚 [AI接口测试] v3 批量删除对话用户接口恒 500(serializer 无 data 即调用 is_valid) https://www.tapd.cn/45245355/s/2178261