This commit is contained in:
anhduy-tech
2026-01-05 00:34:49 +07:00
parent 15bb6720b6
commit e0403055e3
11 changed files with 76 additions and 7 deletions

View File

@@ -1,7 +1,7 @@
import os
import subprocess
from datetime import datetime
from django.db import models
import numpy as np
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
@@ -206,13 +206,23 @@ class DocumentGenerator:
raise ValueError(f"Could not resolve '{lookup_from}'. It is not a valid API parameter or a reference to another data source.")
def _get_value_from_object(self, obj, field_path):
if obj is None:
if not obj:
return None
parts = field_path.split('.')
value = obj
for part in field_path.replace("__", ".").split("."):
for part in parts:
if value is None:
return None
break
# Lấy thuộc tính từ object
value = getattr(value, part, None)
# KIỂM TRA NẾU LÀ QUAN HỆ NGƯỢC (ForeignKey ngược hoặc ManyToMany)
# Trong Django, các quan hệ này trả về một Manager (có method 'all')
if hasattr(value, 'all') and not isinstance(value, models.Model):
value = value.first() # Tự động lấy bản ghi đầu tiên
return value
def fetch_data(self):