使用graphene和django-money (djmoney)为Django提供的GraphQL货币类型。
项目描述
graphene-djmoney
简介
使用graphene和django-money (djmoney)为Django提供的GraphQL货币类型。如果您使用django
、graphene_django
和django-money
,这个库就是为您准备的。
支持环境
- Python 3.7+ (可能也支持更早的版本,需要测试)
- Django 2+
- graphene-django 2+
- django-money 1+
以下是它的使用方式。自动获取此查询
query Products {
products {
id
cost {
...moneyFragment
}
}
}
fragment moneyFragment on Money {
asString # "123.45 USD"
amount # 123.45
amountStr # "123.45"
currency {
code # "USD"
name # "US Dollar"
# These are not as commonly used, see tests:
numeric
symbol
prefix
}
}
使用以下代码
# yourapp/models.py
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models
from djmoney.models.fields import MoneyField
class User(AbstractUser):
pass
class Product(models.Model):
creator = models.ForeignKey(User, related_name="products", on_delete=models.CASCADE)
title = models.CharField(max_length=2000)
cost = MoneyField(
max_digits=settings.CURRENCY_MAX_DIGITS,
decimal_places=settings.CURRENCY_DECIMAL_PLACES,
default_currency=settings.BASE_CURRENCY,
null=True,
blank=True,
)
# yourapp/schema/types.py
import graphene
from graphene_django import DjangoObjectType
from yourapp import models
class Product(DjangoObjectType):
class Meta:
model = models.Product
interfaces = (graphene.relay.Node,)
fields = ("id", "cost")
# yourapp/schema/__init__.py
import graphene
from .. import models
from .types import Product
class Queries(graphene.ObjectType):
products = graphene.List(graphene.NonNull(types.Product), required=True)
def resolve_products(self, info, **kwargs):
return models.Product.objects.all()
schema = graphene.Schema(query=Queries, types=[Product])
# yourapp/settings.py
INSTALLED_APPS += [
"graphene_djmoney",
]
GRAPHENE = {
"SCHEMA": "yourapp.schema.schema",
}
安装
pip install graphene-djmoney
变更日志
0.2.0
- #5, #6, #7 Upgrade to py-moneyed 2.0, add babel format support (new field, `formatted`)
- **Breaking change**: removes `suffix` from schema, since babel doesn't support out of the box.
0.1.3
Initial release, sort of.
贡献
运行测试
poetry run pytest
仍需处理。目前,请提出pull请求或问题。
项目详情
关闭
graphene_djmoney-0.2.3.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 113dd17e878d518fc488a14652f2fb045a4f2e84d8d3739a4809fcc3852404ed |
|
MD5 | ce5530f92b7f9f92b4a2d85e5df3209c |
|
BLAKE2b-256 | 770446abc9ab1d4a21c4c6e7f89267ad7aecad787107e17b0bb9d506c3b8619e |
关闭
graphene_djmoney-0.2.3-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | bad2b75e3ba420f8d2543f030509bc8aff18052823ba15fcfb6796efd777878b |
|
MD5 | f9146a766da1cd5b64a9475dd03aee88 |
|
BLAKE2b-256 | 2489c77b6495474b94555c09272562734933729cc7cf8ca6de697bb1c998c149 |