本文整理汇总了Python中rest_framework.fields.BooleanField方法的典型用法代码示例。如果您正苦于以下问题:Python fields.BooleanField方法的具体用法?Python fields.BooleanField怎么用?Python fields.BooleanField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.fields
的用法示例。
在下文中一共展示了fields.BooleanField方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _filters_to_query
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import BooleanField [as 别名]
def _filters_to_query(self, includes, excludes, serializer, q=None):
"""
Construct Django Query object from request.
Arguments are dictionaries, which will be passed to Q() as kwargs.
e.g.
includes = { 'foo' : 'bar', 'baz__in' : [1, 2] }
produces:
Q(foo='bar', baz__in=[1, 2])
Arguments:
includes: TreeMap representing inclusion filters.
excludes: TreeMap representing exclusion filters.
serializer: serializer instance of top-level object
q: Q() object (optional)
Returns:
Q() instance or None if no inclusion or exclusion filters
were specified.
"""
def rewrite_filters(filters, serializer):
out = {}
for k, node in six.iteritems(filters):
filter_key, field = node.generate_query_key(serializer)
if isinstance(field, (BooleanField, NullBooleanField)):
node.value = is_truthy(node.value)
out[filter_key] = node.value
return out
q = q or Q()
if not includes and not excludes:
return None
if includes:
includes = rewrite_filters(includes, serializer)
q &= Q(**includes)
if excludes:
excludes = rewrite_filters(excludes, serializer)
for k, v in six.iteritems(excludes):
q &= ~Q(**{k: v})
return q
示例2: build_standard_field
# 需要导入模块: from rest_framework import fields [as 别名]
# 或者: from rest_framework.fields import BooleanField [as 别名]
def build_standard_field(self, field_name, model_field):
field_mapping = ClassLookupDict(self.serializer_field_mapping)
field_class = field_mapping[model_field]
field_kwargs = get_field_kwargs(field_name, model_field)
if 'choices' in field_kwargs:
# Fields with choices get coerced into `ChoiceField`
# instead of using their regular typed field.
field_class = self.serializer_choice_field
# Some model fields may introduce kwargs that would not be valid
# for the choice field. We need to strip these out.
# Eg. models.DecimalField(max_digits=3, decimal_places=1, choices=DECIMAL_CHOICES)
valid_kwargs = set((
'read_only', 'write_only',
'required', 'default', 'initial', 'source',
'label', 'help_text', 'style',
'error_messages', 'validators', 'allow_null', 'allow_blank',
'choices'
))
for key in list(field_kwargs.keys()):
if key not in valid_kwargs:
field_kwargs.pop(key)
if 'regex' in field_kwargs:
field_class = drf_fields.RegexField
if not issubclass(field_class, drfm_fields.DocumentField):
# `model_field` is only valid for the fallback case of
# `ModelField`, which is used when no other typed field
# matched to the model field.
field_kwargs.pop('model_field', None)
if not issubclass(field_class, drf_fields.CharField) and not issubclass(field_class, drf_fields.ChoiceField):
# `allow_blank` is only valid for textual fields.
field_kwargs.pop('allow_blank', None)
if field_class is drf_fields.BooleanField and field_kwargs.get('allow_null', False):
field_kwargs.pop('allow_null', None)
field_kwargs.pop('default', None)
field_class = drf_fields.NullBooleanField
return field_class, field_kwargs