px
All checks were successful
Deploy to Server / deploy (push) Successful in 24s

This commit is contained in:
jeremygan2021
2026-02-25 01:28:08 +08:00
parent 8298eb6add
commit cf063707a3
4 changed files with 34 additions and 26 deletions

View File

@@ -202,16 +202,17 @@ class TopicAdmin(OrderableAdminMixin, ModelAdmin):
inlines = [TopicMediaInline, ReplyInline]
actions = ['reset_ordering']
@admin.action(description="重置排序 (按发布时间倒序)")
@admin.action(description="重置排序 (0,1,2... 新帖子在前)")
def reset_ordering(self, request, queryset):
"""
将所有帖子按时间倒序重新分配order值order = -id
将所有帖子按时间倒序重新分配order值 (0, 1, 2, ...)
"""
all_objects = Topic.objects.all().order_by('-id')
for obj in all_objects:
obj.order = -obj.id
obj.save(update_fields=['order'])
self.message_user(request, f"成功重置了 {all_objects.count()} 个帖子的排序权重(新帖子在前)。")
all_objects = Topic.objects.all().order_by('-created_at')
for index, obj in enumerate(all_objects):
if obj.order != index:
obj.order = index
obj.save(update_fields=['order'])
self.message_user(request, f"成功重置了 {all_objects.count()} 个帖子的排序权重从0开始")
fieldsets = (
('帖子内容', {