1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| class IncomeExpporSerializerMetaclass(ExpporSerializerMetaclass):
# (ラベル, フィールド名, フィールドクラス名)
EXPORT_META = [
("会社コード", "order__company_code", "CharField"),
....
]
# フィールドクラスのデフォルト値
FIELD_CLASS_DEFAULTS = dict(
CharField=dict(read_only=True),
DecimalField=dict(max_digits=12, decimal_places=1, read_only=True),
)
@classmethod
def annotate(cls, queryset):
""" クエリセットを適切にアノテートする"""
def _ann(item):
label, name, klass = item
if name.startswith("order__"):
# リレーション
return (name, F(name))
if name.startswith("meme__"):
# 固定値
return (name, Value(""))
# その他はモデルフィールドなのでアノテートしない
return (None, None)
ann = dict(filter(lambda i: i[1] is not None, map(_ann, cls.EXPORT_META)))
return queryset.annotate(**ann)
|