This repository has been archived on 2025-11-04. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
csmb-housewars/housewars/utils/unpack_values.py

24 lines
646 B
Python

from django.db.models import QuerySet
from typing import List
def unpack_values(queryset: QuerySet, headers: List[str]) -> List[List]:
"""Unpacks a queryset into a 2D array
:param queryset: The queryset that will be unpacked
:param headers: A list of the desired headers of the queryset
:returns A 2D array of headers, then rows
"""
data = []
data.append(headers)
for row in queryset:
values = []
for field in headers:
value = getattr(row, field)
if value is None:
value = ''
values.append(value)
data.append(values)
return data