【问题标题】:How to format dates and datetime in graphene如何在石墨烯中格式化日期和日期时间
【发布时间】:2019-09-26 09:07:01
【问题描述】:

我希望石墨烯输出正常的日期/日期时间格式。默认情况下它返回:

`"createdAt": "2019-06-11T05:48:11.023255+00:00",`

我希望它返回这个:

`"createdAt": "11.06.2019 11:48:11",`

石墨烯字典中似乎没有这样的选项,但我仍然想使用graphene_django.DjangoObjectType 来使用它的 Meta 类,而不是用graphene.ObjectType 重写所有字段

【问题讨论】:

    标签: django date datetime graphene-django


    【解决方案1】:

    您可以使用DjangoObjectType 覆盖任何字段。您的代码可能如下所示。

    class CustomType(DjangoObjectType):
    
       class Meta:
          model = CustomModel
    
    
       created_at = graphene.String()
    
       def resolve_created_at(self, info):
          return str(self.created_at.replace(microsecond=0))
    

    更多详情请查看docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多