Как сделать цикл в строке excel с помощью xlwt и django?

Здесь я пытаюсь экспортировать некоторые отчеты о клиентах в формат excel с помощью xlwt. При экспорте у меня возникает проблема с отображением сопутствующих товаров клиента.

В настоящее время я получаю следующий ответ:

enter image description here

Ответ, который я хочу получить:

enter image description here

    response = HttpResponse(content_type="application/ms-excel")
    response["Content-Disposition"] = 'attachment; filename="report_detail.xls"'

    wb = xlwt.Workbook(encoding="utf-8")
    ws = wb.add_sheet(f"Report Detail", cell_overwrite_ok=True)

    row_num = 0

    font_style = xlwt.XFStyle()
    font_style.font.bold = True

    columns = [
        "Customer ID",
        "Products"]
    for col_num in range(len(columns)):
         ws.write(row_num, col_num, columns[col_num], font_style)

    rows = []
    for obj in qs:
        rows.append(obj.customer_id)
        prods = [(p.name, p.code) for p in obj.products.all()]
        rows.append(", ".join(map(str, prods)))

    for i, e in enumerate(rows, start=2):
        ws.write(int(i / 2), int(i) % 2, e)


    wb.save(response)
    return response
        
Вернуться на верх