我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用django.contrib.postgres.fields.HStoreField()。
def from_field(cls, field): if POSTGRES_AVAILABLE: if isinstance(field, JSONField) or isinstance(field, HStoreField): return cls(verbose_name=title(field.verbose_name))
def __init__(self, fields=(), keys=(), max_value_length=25, require_all_fields=False, **kwargs): super(HStoreField, self).__init__(**kwargs) self.fields = fields self.keys = keys self.max_value_length = max_value_length self.require_all_fields = require_all_fields
def formfield(self, **kwargs): if self.fields or self.keys: defaults = { 'form_class': NestedFormField, 'fields': self.fields, 'keys': list(self.keys), 'require_all_fields': self.require_all_fields, 'max_value_length': self.max_value_length } defaults.update(kwargs) else: defaults = kwargs return super(HStoreField, self).formfield(**defaults)
def test_should_query_postgres_fields(): from django.contrib.postgres.fields import IntegerRangeField, ArrayField, JSONField, HStoreField class Event(models.Model): ages = IntegerRangeField(help_text='The age ranges') data = JSONField(help_text='Data') store = HStoreField() tags = ArrayField(models.CharField(max_length=50)) class EventType(DjangoObjectType): class Meta: model = Event class Query(graphene.ObjectType): event = graphene.Field(EventType) def resolve_event(self, info): return Event( ages=(0, 10), data={'angry_babies': True}, store={'h': 'store'}, tags=['child', 'angry', 'babies'] ) schema = graphene.Schema(query=Query) query = ''' query myQuery { event { ages tags data store } } ''' expected = { 'event': { 'ages': [0, 10], 'tags': ['child', 'angry', 'babies'], 'data': '{"angry_babies": true}', 'store': '{"h": "store"}', }, } result = schema.execute(query) assert not result.errors assert result.data == expected