update. 更新数据填充脚本

This commit is contained in:
ngfchl
2026-04-05 12:55:57 +08:00
parent 5174407da5
commit 60366b7eb1
4 changed files with 29 additions and 12 deletions
+22 -7
View File
@@ -3,6 +3,8 @@ import random
import sys
import django
from loguru import logger
from pypinyin import lazy_pinyin, Style
# 配置 Django 环境
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
@@ -17,18 +19,31 @@ from users.models import User
# 初始化 Faker (zh_CN 表示生成中文数据)
fake = Faker('zh_CN')
NEWS_SECTIONS = [
{"title": "消防要闻", "order": 1, "visible": True, },
{"title": "基层动态", "order": 2, "visible": True, },
{"title": "国务院新闻", "order": 3, "visible": True, },
]
def seed_news(count=20):
print(f"🚀 开始生成 {count} 条富文本新闻数据...")
logger.info(f"🚀 开始生成 {count} 条富文本新闻数据...")
# --- 1. 获取关联数据 ---
sections = list(NewsSection.objects.all())
users = list(User.objects.all())
if not sections:
print("❌ 错误:数据库中没有找到任何板块 (NewsSection)")
return
logger.warning("❌ 错误:数据库中没有找到任何板块 (NewsSection)")
for sec in NEWS_SECTIONS:
title = sec["title"]
NewsSection.objects.create(
title=title,
code="-".join(lazy_pinyin(title, style=Style.NORMAL)),
order=sec["order"],
visible=sec["visible"],
)
sections = list(NewsSection.objects.all())
created_count = 0
for i in range(count):
@@ -90,10 +105,10 @@ def seed_news(count=20):
)
created_count += 1
print(f" - [{section.title}] {news.title}")
logger.info(f" - [{section.title}] {news.title}")
print(f"✅ 成功生成 {created_count} 条富文本新闻数据!")
logger.info(f"✅ 成功生成 {created_count} 条富文本新闻数据!")
if __name__ == "__main__":
seed_news(120)
seed_news(600)