This commit is contained in:
@@ -202,16 +202,17 @@ class TopicAdmin(OrderableAdminMixin, ModelAdmin):
|
|||||||
inlines = [TopicMediaInline, ReplyInline]
|
inlines = [TopicMediaInline, ReplyInline]
|
||||||
actions = ['reset_ordering']
|
actions = ['reset_ordering']
|
||||||
|
|
||||||
@admin.action(description="重置排序 (按发布时间倒序)")
|
@admin.action(description="重置排序 (0,1,2... 新帖子在前)")
|
||||||
def reset_ordering(self, request, queryset):
|
def reset_ordering(self, request, queryset):
|
||||||
"""
|
"""
|
||||||
将所有帖子按时间倒序重新分配order值(order = -id)
|
将所有帖子按时间倒序重新分配order值 (0, 1, 2, ...)
|
||||||
"""
|
"""
|
||||||
all_objects = Topic.objects.all().order_by('-id')
|
all_objects = Topic.objects.all().order_by('-created_at')
|
||||||
for obj in all_objects:
|
for index, obj in enumerate(all_objects):
|
||||||
obj.order = -obj.id
|
if obj.order != index:
|
||||||
|
obj.order = index
|
||||||
obj.save(update_fields=['order'])
|
obj.save(update_fields=['order'])
|
||||||
self.message_user(request, f"成功重置了 {all_objects.count()} 个帖子的排序权重(新帖子在前)。")
|
self.message_user(request, f"成功重置了 {all_objects.count()} 个帖子的排序权重(从0开始)。")
|
||||||
|
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
('帖子内容', {
|
('帖子内容', {
|
||||||
|
|||||||
@@ -142,12 +142,19 @@ class Topic(models.Model):
|
|||||||
order = models.IntegerField(default=0, verbose_name="排序")
|
order = models.IntegerField(default=0, verbose_name="排序")
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
|
# 记录是否为新对象,因为super().save后pk就有了
|
||||||
is_new = self.pk is None
|
is_new = self.pk is None
|
||||||
|
|
||||||
|
# 第一次保存,先入库
|
||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
# 如果是新创建,且 order 默认为 0(未指定)
|
||||||
if is_new and getattr(self, 'order', 0) == 0:
|
if is_new and getattr(self, 'order', 0) == 0:
|
||||||
# 默认使用 -id 作为排序值,确保新帖子默认排在前面(order越小越靠前)
|
# 将所有其他帖子的 order + 1,腾出 0 的位置
|
||||||
Topic.objects.filter(pk=self.pk).update(order=-self.pk)
|
Topic.objects.exclude(pk=self.pk).filter(order__gte=0).update(order=models.F('order') + 1)
|
||||||
self.order = -self.pk
|
# 确保自己是 0
|
||||||
|
Topic.objects.filter(pk=self.pk).update(order=0)
|
||||||
|
self.order = 0
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.title
|
return self.title
|
||||||
|
|||||||
@@ -259,8 +259,8 @@ class TopicViewSet(viewsets.ModelViewSet):
|
|||||||
filter_backends = [filters.SearchFilter, filters.OrderingFilter, DjangoFilterBackend]
|
filter_backends = [filters.SearchFilter, filters.OrderingFilter, DjangoFilterBackend]
|
||||||
search_fields = ['title', 'content']
|
search_fields = ['title', 'content']
|
||||||
filterset_fields = ['category', 'is_pinned']
|
filterset_fields = ['category', 'is_pinned']
|
||||||
ordering_fields = ['created_at', 'view_count']
|
ordering_fields = ['created_at', 'view_count', 'order']
|
||||||
ordering = ['-is_pinned', '-created_at']
|
ordering = ['order', '-is_pinned', '-created_at']
|
||||||
|
|
||||||
def perform_create(self, serializer):
|
def perform_create(self, serializer):
|
||||||
user = get_current_wechat_user(self.request)
|
user = get_current_wechat_user(self.request)
|
||||||
|
|||||||
@@ -101,19 +101,7 @@ DATABASES = {
|
|||||||
|
|
||||||
#从环境变量获取数据库配置 (Docker 环境会自动注入这些变量。
|
#从环境变量获取数据库配置 (Docker 环境会自动注入这些变量。
|
||||||
|
|
||||||
DB_HOST = os.environ.get('DB_HOST', '6.6.6.66')
|
# DB_HOST = os.environ.get('DB_HOST', '6.6.6.66')
|
||||||
if DB_HOST:
|
|
||||||
DATABASES['default'] = {
|
|
||||||
'ENGINE': 'django.db.backends.postgresql',
|
|
||||||
'NAME': os.environ.get('DB_NAME', 'market'),
|
|
||||||
'USER': os.environ.get('DB_USER', 'market'),
|
|
||||||
'PASSWORD': os.environ.get('DB_PASSWORD', '123market'),
|
|
||||||
'HOST': DB_HOST,
|
|
||||||
'PORT': os.environ.get('DB_PORT', '5432'),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
# DB_HOST = os.environ.get('DB_HOST', '121.43.104.161')
|
|
||||||
# if DB_HOST:
|
# if DB_HOST:
|
||||||
# DATABASES['default'] = {
|
# DATABASES['default'] = {
|
||||||
# 'ENGINE': 'django.db.backends.postgresql',
|
# 'ENGINE': 'django.db.backends.postgresql',
|
||||||
@@ -121,10 +109,22 @@ if DB_HOST:
|
|||||||
# 'USER': os.environ.get('DB_USER', 'market'),
|
# 'USER': os.environ.get('DB_USER', 'market'),
|
||||||
# 'PASSWORD': os.environ.get('DB_PASSWORD', '123market'),
|
# 'PASSWORD': os.environ.get('DB_PASSWORD', '123market'),
|
||||||
# 'HOST': DB_HOST,
|
# 'HOST': DB_HOST,
|
||||||
# 'PORT': os.environ.get('DB_PORT', '6433'),
|
# 'PORT': os.environ.get('DB_PORT', '5432'),
|
||||||
# }
|
# }
|
||||||
|
|
||||||
|
|
||||||
|
DB_HOST = os.environ.get('DB_HOST', '121.43.104.161')
|
||||||
|
if DB_HOST:
|
||||||
|
DATABASES['default'] = {
|
||||||
|
'ENGINE': 'django.db.backends.postgresql',
|
||||||
|
'NAME': os.environ.get('DB_NAME', 'market'),
|
||||||
|
'USER': os.environ.get('DB_USER', 'market'),
|
||||||
|
'PASSWORD': os.environ.get('DB_PASSWORD', '123market'),
|
||||||
|
'HOST': DB_HOST,
|
||||||
|
'PORT': os.environ.get('DB_PORT', '6433'),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# Password validation
|
# Password validation
|
||||||
# https://docs.djangoproject.com/en/6.0/ref/settings/#auth-password-validators
|
# https://docs.djangoproject.com/en/6.0/ref/settings/#auth-password-validators
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user