fix: 前端单独加载批注(GenericForeignKey 无法用 related_name)

This commit is contained in:
maoshen
2026-04-14 12:22:13 +00:00
parent b2d3dff75e
commit 0285ed25b1
2 changed files with 22 additions and 1 deletions

View File

@@ -281,6 +281,15 @@
scores: { quality: '', efficiency: '', creativity: '', learning: '' }
};
async function loadComments(contentType, objectId) {
try {
const res = await fetch(`${API_BASE}/comments/by_content/?content_type=${contentType}&object_id=${objectId}`);
return await res.json();
} catch (e) {
return [];
}
}
async function loadData() {
try {
const [taskStatsRes, tasksRes, diaryStatsRes, entriesRes, expStatsRes, experiencesRes] = await Promise.all([
@@ -297,6 +306,18 @@
state.allEntries = await entriesRes.json();
state.expStats = await expStatsRes.json();
state.allExperiences = await experiencesRes.json();
// 加载所有批注
for (const entry of state.allEntries) {
entry.comments = await loadComments('diary', entry.id);
}
for (const task of state.allTasks) {
task.comments = await loadComments('task', task.id);
}
for (const exp of state.allExperiences) {
exp.comments = await loadComments('experience', exp.id);
}
render();
} catch (error) {
document.getElementById('app').innerHTML = `<div class="error">加载失败:${error.message}</div>`;