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 = (
|
||||
('帖子内容', {
|
||||
|
||||
@@ -145,8 +145,9 @@ class Topic(models.Model):
|
||||
is_new = self.pk is None
|
||||
super().save(*args, **kwargs)
|
||||
if is_new and getattr(self, 'order', 0) == 0:
|
||||
Topic.objects.filter(pk=self.pk).update(order=self.pk)
|
||||
self.order = self.pk
|
||||
# 默认使用 -id 作为排序值,确保新帖子默认排在前面(order越小越靠前)
|
||||
Topic.objects.filter(pk=self.pk).update(order=-self.pk)
|
||||
self.order = -self.pk
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
Reference in New Issue
Block a user