在我的 Revenue 字段的 graphql api 中使用来自常量的 Currency 枚举。
from graphene import Enum
CurrencyEnum = Enum('Currency', CURRENCY_CHOICES)
class RevenueMetrics:
revenue = graphene.Float(description='Revenue', currency=CurrencyEnum(required=True))
...
但是现在
CURRENCY_CHOICES
常数已经移动到DB(货币表)。这有充分的理由(与 graphql 无关)。
我无法弄清楚如何使用货币表的 QuerySet 动态填充来创建 CurrencyEnum 类。有一套最新的货币。(我想在计划的重组中还有另一个问题)。
graphene==2.1.8
graphene-django==2.13.0
我这样试过:
def dcm():
return Enum('Currency', tuple((c, c) for c in Currency.objects.values_list('code', flat=True)))
revenue = graphene.Float(description='Revenue', currency=dcm()(required=True))
但它仍然需要模式重新生成。