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

This commit is contained in:
jeremygan2021
2026-02-25 00:53:01 +08:00
parent 05299060dc
commit 8298eb6add
2 changed files with 9 additions and 8 deletions

View File

@@ -202,16 +202,16 @@ class TopicAdmin(OrderableAdminMixin, ModelAdmin):
inlines = [TopicMediaInline, ReplyInline]
actions = ['reset_ordering']
@admin.action(description="重置排序 (按ID顺序)")
@admin.action(description="重置排序 (按发布时间倒序)")
def reset_ordering(self, request, queryset):
"""
将所有帖子按ID顺序重新分配order值
将所有帖子按时间倒序重新分配order值order = -id
"""
all_objects = Topic.objects.all().order_by('id')
for index, obj in enumerate(all_objects, start=1):
obj.order = index
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()} 个帖子的排序权重。")
self.message_user(request, f"成功重置了 {all_objects.count()} 个帖子的排序权重(新帖子在前)")
fieldsets = (
('帖子内容', {