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