我们从Python开源项目中,提取了以下41个代码示例,用于说明如何使用django.forms.NullBooleanField()。
def _check_null(self, **kwargs): if getattr(self, 'null', False): return [ checks.Error( 'BooleanFields do not accept null values.', hint='Use a NullBooleanField instead.', obj=self, id='fields.E110', ) ] else: return []
def __init__(self, *args, **kwargs): kwargs['null'] = True kwargs['blank'] = True super(NullBooleanField, self).__init__(*args, **kwargs)
def deconstruct(self): name, path, args, kwargs = super(NullBooleanField, self).deconstruct() del kwargs['null'] del kwargs['blank'] return name, path, args, kwargs
def get_internal_type(self): return "NullBooleanField"
def get_prep_lookup(self, lookup_type, value): # Special-case handling for filters coming from a Web request (e.g. the # admin interface). Only works for scalar values (not lists). If you're # passing in a list, you might as well make things the right type when # constructing the list. if value in ('1', '0'): value = bool(int(value)) return super(NullBooleanField, self).get_prep_lookup(lookup_type, value)
def formfield(self, **kwargs): defaults = { 'form_class': forms.NullBooleanField, 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} defaults.update(kwargs) return super(NullBooleanField, self).formfield(**defaults)
def test_should_nullboolean_convert_boolean(): field = assert_conversion(forms.NullBooleanField, graphene.Boolean) assert not isinstance(field.type, NonNull)
def get_prep_value(self, value): value = super(NullBooleanField, self).get_prep_value(value) if value is None: return None return self.to_python(value)
def get_custom_fields_for_model(content_type, filterable_only=False, bulk_edit=False): """ Retrieve all CustomFields applicable to the given ContentType """ field_dict = OrderedDict() kwargs = {'obj_type': content_type} if filterable_only: kwargs['is_filterable'] = True custom_fields = CustomField.objects.filter(**kwargs) for cf in custom_fields: field_name = 'cf_{}'.format(str(cf.name)) # Integer if cf.type == CF_TYPE_INTEGER: field = forms.IntegerField(required=cf.required, initial=cf.default) # Boolean elif cf.type == CF_TYPE_BOOLEAN: choices = ( (None, '---------'), (1, 'True'), (0, 'False'), ) if cf.default.lower() in ['true', 'yes', '1']: initial = 1 elif cf.default.lower() in ['false', 'no', '0']: initial = 0 else: initial = None field = forms.NullBooleanField(required=cf.required, initial=initial, widget=forms.Select(choices=choices)) # Date elif cf.type == CF_TYPE_DATE: field = forms.DateField(required=cf.required, initial=cf.default, help_text="Date format: YYYY-MM-DD") # Select elif cf.type == CF_TYPE_SELECT: choices = [(cfc.pk, cfc) for cfc in cf.choices.all()] if not cf.required or bulk_edit or filterable_only: choices = [(None, '---------')] + choices field = forms.TypedChoiceField(choices=choices, coerce=int, required=cf.required) # URL elif cf.type == CF_TYPE_URL: field = LaxURLField(required=cf.required, initial=cf.default) # Text else: field = forms.CharField(max_length=255, required=cf.required, initial=cf.default) field.model = cf field.label = cf.label if cf.label else cf.name.replace('_', ' ').capitalize() if cf.description: field.help_text = cf.description field_dict[field_name] = field return field_dict