update. 更新数据填充脚本
This commit is contained in:
+1
-1
@@ -121,4 +121,4 @@ def seed_articles(count=20):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
seed_articles(120)
|
||||
seed_articles(1200)
|
||||
|
||||
+22
-7
@@ -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)
|
||||
|
||||
+2
-1
@@ -125,7 +125,7 @@ ASGI_APPLICATION = 'server.asgi.application'
|
||||
|
||||
if env('DB_REMOTE', default=False):
|
||||
# mysql_connect = dj_database_url.parse(os.getenv('MYSQL_CONNECTION'), conn_max_age=0)
|
||||
# print("数据库连接信息", mysql_connect)
|
||||
logger.info("正在使用 PGSQL 数据库连接信息")
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': env('DB_ENGINE', default='dj_db_conn_pool.backends.postgresql'),
|
||||
@@ -147,6 +147,7 @@ if env('DB_REMOTE', default=False):
|
||||
},
|
||||
}
|
||||
else:
|
||||
logger.info("正在使用本地 SQLite 数据库", )
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
|
||||
+4
-3
@@ -3,6 +3,7 @@ import sys
|
||||
|
||||
import django
|
||||
from faker import Faker
|
||||
from loguru import logger
|
||||
|
||||
# 配置 Django 环境
|
||||
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
||||
@@ -16,7 +17,7 @@ fake = Faker('zh_CN')
|
||||
|
||||
|
||||
def seed_users(count=5):
|
||||
print(f"正在生成 {count} 个用户...")
|
||||
logger.info(f"正在生成 {count} 个用户...")
|
||||
|
||||
users = []
|
||||
for i in range(count):
|
||||
@@ -37,9 +38,9 @@ def seed_users(count=5):
|
||||
last_name=fake.last_name()
|
||||
)
|
||||
users.append(user)
|
||||
print(f" - 创建用户: {username} (密码: password123)")
|
||||
logger.info(f" - 创建用户: {username} (密码: password123)")
|
||||
|
||||
print("用户生成完毕!")
|
||||
logger.info("用户生成完毕!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user