fix: 前端单独加载批注(GenericForeignKey 无法用 related_name)
This commit is contained in:
@@ -43,7 +43,7 @@ class DiaryEntrySerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = DiaryEntry
|
model = DiaryEntry
|
||||||
fields = '__all__'
|
fields = ['id', 'progresses', 'linked_tasks', 'experiences', 'comments', 'date', 'title', 'content', 'completed_tasks', 'learned', 'problems', 'reflections', 'improvements', 'plans', 'created_at', 'updated_at']
|
||||||
|
|
||||||
class TaskSerializer(serializers.ModelSerializer):
|
class TaskSerializer(serializers.ModelSerializer):
|
||||||
status_display = serializers.CharField(source='get_status_display', read_only=True)
|
status_display = serializers.CharField(source='get_status_display', read_only=True)
|
||||||
|
|||||||
@@ -281,6 +281,15 @@
|
|||||||
scores: { quality: '', efficiency: '', creativity: '', learning: '' }
|
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() {
|
async function loadData() {
|
||||||
try {
|
try {
|
||||||
const [taskStatsRes, tasksRes, diaryStatsRes, entriesRes, expStatsRes, experiencesRes] = await Promise.all([
|
const [taskStatsRes, tasksRes, diaryStatsRes, entriesRes, expStatsRes, experiencesRes] = await Promise.all([
|
||||||
@@ -297,6 +306,18 @@
|
|||||||
state.allEntries = await entriesRes.json();
|
state.allEntries = await entriesRes.json();
|
||||||
state.expStats = await expStatsRes.json();
|
state.expStats = await expStatsRes.json();
|
||||||
state.allExperiences = await experiencesRes.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();
|
render();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
document.getElementById('app').innerHTML = `<div class="error">加载失败:${error.message}</div>`;
|
document.getElementById('app').innerHTML = `<div class="error">加载失败:${error.message}</div>`;
|
||||||
|
|||||||
Reference in New Issue
Block a user