我们从Python开源项目中,提取了以下31个代码示例,用于说明如何使用django.db.models.fields.BooleanField()。
def get_django_field_map(self): from django.db.models import fields as djf return [ (djf.AutoField, PrimaryKeyField), (djf.BigIntegerField, BigIntegerField), # (djf.BinaryField, BlobField), (djf.BooleanField, BooleanField), (djf.CharField, CharField), (djf.DateTimeField, DateTimeField), # Extends DateField. (djf.DateField, DateField), (djf.DecimalField, DecimalField), (djf.FilePathField, CharField), (djf.FloatField, FloatField), (djf.IntegerField, IntegerField), (djf.NullBooleanField, partial(BooleanField, null=True)), (djf.TextField, TextField), (djf.TimeField, TimeField), (djf.related.ForeignKey, ForeignKeyField), ]
def test_builtin_fields(self): self.assertEqual( views.get_readable_field_data_type(fields.BooleanField()), _('Boolean (Either True or False)') )
def set_boolean(instance, field): """ BooleanField """ possibles = [True, False] possibles.extend(get_field_choices(field)) setattr(instance, field.name, random.choice(possibles))
def filter_fields_to_type(klass, query_dict): reserved_fields = ['order_by', 'format', 'limit', 'offset'] q = QuerySet(klass) query = dict(query_dict) fields = {} for field in q.model._meta.fields: fields[field.column] = field # Remove the reserved fields we know about. for field in query.keys(): if field in reserved_fields: del query[field] # This will error if it find an unknown field and cause the standard tasty pie query to run. for field in query.keys(): try: field_type = type(fields[field]) value = query[field] if field_type == django_fields.AutoField or field_type == django_fields.IntegerField: value = int(value) elif field_type == django_fields.BooleanField: value = (value.lower() == 'true') query[field] = value except KeyError: pass return query # monkey-patch ResourceOptions to have a default-empty readonly list
def clean(self): super(PagePermissionInlineAdminForm, self).clean() for field in self.Meta.model._meta.fields: if not isinstance(field, BooleanField) or not field.name.startswith('can_'): continue name = field.name self.cleaned_data[name] = self.cleaned_data.get(name, False) can_add = self.cleaned_data['can_add'] can_edit = self.cleaned_data['can_change'] # check if access for childrens, or descendants is granted if can_add and self.cleaned_data['grant_on'] == ACCESS_PAGE: # this is a missconfiguration - user can add/move page to current # page but after he does this, he will not have permissions to # access this page anymore, so avoid this raise forms.ValidationError(_("Add page permission requires also " "access to children, or descendants, otherwise added page " "can't be changed by its creator.")) if can_add and not can_edit: raise forms.ValidationError(_('Add page permission also requires edit page permission.')) # TODO: finish this, but is it really required? might be nice to have # check if permissions assigned in cms are correct, and display # a message if not - correctness mean: if user has add permission to # page, but he doesn't have auth permissions to add page object, # display warning return self.cleaned_data
def save(self, commit=True): """ Makes sure the boolean fields are set to False if they aren't available in the form. """ instance = super(PagePermissionInlineAdminForm, self).save(commit=False) for field in self._meta.model._meta.fields: if isinstance(field, BooleanField) and field.name.startswith('can_'): setattr(instance, field.name, self.cleaned_data.get(field.name, False)) if commit: instance.save() return instance
def output_field(self): return fields.BooleanField()