This commit is contained in:
@@ -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 = (
|
||||
('帖子内容', {
|
||||
|
||||
Reference in New Issue
Block a user