我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.db.models.BooleanField()。
def display_for_field(value, field, empty_value_display): from django.contrib.admin.templatetags.admin_list import _boolean_icon if getattr(field, 'flatchoices', None): return dict(field.flatchoices).get(value, empty_value_display) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return _boolean_icon(value) elif value is None: return empty_value_display elif isinstance(field, models.DateTimeField): return formats.localize(timezone.template_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, (models.IntegerField, models.FloatField)): return formats.number_format(value) elif isinstance(field, models.FileField) and value: return format_html('<a href="{}">{}</a>', value.url, value) else: return smart_text(value)
def display_for_field(value, field, empty_value_display): from django.contrib.admin.templatetags.admin_list import _boolean_icon if getattr(field, 'flatchoices', None): return dict(field.flatchoices).get(value, empty_value_display) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return _boolean_icon(value) elif value is None: return empty_value_display elif isinstance(field, models.DateTimeField): return formats.localize(timezone.template_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, (models.IntegerField, models.FloatField)): return formats.number_format(value) elif isinstance(field, models.FileField) and value: return format_html('<a href="{}">{}</a>', value.url, value) else: return display_for_value(value, empty_value_display)
def display_for_field(value, field): from xadmin.views.list import EMPTY_CHANGELIST_VALUE if field.flatchoices: return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return boolean_icon(value) elif value is None: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateTimeField): return formats.localize(tz_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, models.FloatField): return formats.number_format(value) elif isinstance(field.rel, models.ManyToManyRel): return ', '.join([smart_text(obj) for obj in value.all()]) else: return smart_text(value)
def display_for_field(value, field): from xadmin.views.list import EMPTY_CHANGELIST_VALUE if field.flatchoices: return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return boolean_icon(value) elif value is None: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateTimeField): return formats.localize(tz_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, models.FloatField): return formats.number_format(value) elif isinstance(field.rel, models.ManyToManyRel): return ', '.join([smart_unicode(obj) for obj in value.all()]) else: return smart_unicode(value)
def display_for_field(value, field): from django.contrib.admin.templatetags.admin_list import _boolean_icon from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE if field.flatchoices: return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return _boolean_icon(value) elif value is None: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateTimeField): return formats.localize(timezone.template_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, models.FloatField): return formats.number_format(value) elif isinstance(field, models.FileField) and value: return format_html('<a href="{}">{}</a>', value.url, value) else: return smart_text(value)
def test_basic_model_message(self): class MockMessage(DjangoProtoRPCMessage): class Meta: model = DummyModel self.assertIsSubclass(MockMessage, messages.Message) self.assertIsInstance(MockMessage.foo, messages.StringField) self.assertIsInstance(MockMessage.whizz_id, messages.IntegerField) self.assertIsInstance(MockMessage.bar_id, messages.IntegerField) self.assertIsInstance(MockMessage.default, messages.BooleanField) self.assertTrue(MockMessage.foo.required) self.assertFalse(MockMessage.default.required) self.assertFalse(hasattr(MockMessage, 'bar')) self.assertFalse(hasattr(MockMessage, 'foobars')) self.assertFalse(hasattr(MockMessage, 'foobar_ids'))
def test_basic_model_no_required(self): class MockMessage(DjangoProtoRPCMessage): class Meta: model = DummyModel never_required = True self.assertIsSubclass(MockMessage, messages.Message) self.assertIsInstance(MockMessage.foo, messages.StringField) self.assertIsInstance(MockMessage.whizz_id, messages.IntegerField) self.assertIsInstance(MockMessage.bar_id, messages.IntegerField) self.assertIsInstance(MockMessage.default, messages.BooleanField) self.assertFalse(MockMessage.foo.required) self.assertFalse(MockMessage.default.required) self.assertFalse(hasattr(MockMessage, 'bar')) self.assertFalse(hasattr(MockMessage, 'foobars')) self.assertFalse(hasattr(MockMessage, 'foobar_ids'))
def display_for_field(value, field): from xadmin.views.list import EMPTY_CHANGELIST_VALUE if field.flatchoices: return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return boolean_icon(value) elif value is None: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateTimeField): return formats.localize(tz_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, models.FloatField): return formats.number_format(value) elif isinstance(field.rel, models.ManyToManyRel): return ', '.join([smart_unicode(obj) for obj in value.all()]) else: return value#smart_unicode(value)
def get_user_queryset(self): qs = super(UserViewSet, self).get_user_queryset() if self.action == 'retrieve': if self.request.user.is_authenticated(): qs = qs.annotate( followed=models.ExpressionWrapper( models.Count( models.Subquery( User.following.through.objects.filter( to_user_id=self.request.user.id, from_user_id=models.OuterRef('id') ).values('id') ) ), output_field=models.BooleanField() ) ) return qs
def get_queryset(self): user = self.get_user_object() try: rel_field = getattr(user, self.allowed_actions[self.action]) except KeyError: raise NotFound if self.request.user.is_authenticated(): rel_field = rel_field.annotate( followed=models.ExpressionWrapper( models.Count( models.Subquery( User.following.through.objects.filter( to_user_id=self.request.user.id, from_user_id=models.OuterRef('id') ).values('id') ) ), output_field=models.BooleanField() ) ) return rel_field.order_by('id')
def test_get_field(self): """ The _get_field method should return a form field instance """ field_1 = self.omniform._get_field('title') field_2 = self.omniform._get_field('agree') field_3 = self.omniform._get_field('fictional') self.assertIsInstance(field_1, forms.CharField) self.assertIsInstance(field_1.widget, forms.TextInput) self.assertEqual(field_1.label, 'Please give us a title') self.assertTrue(field_1.required) self.assertIsInstance(field_2, forms.BooleanField) self.assertIsInstance(field_2.widget, forms.CheckboxInput) self.assertEqual(field_2.label, 'Please agree') self.assertTrue(field_2.required) self.assertIsNone(field_3)
def check_boolean_field_default_value(): """ Checks if there are any BooleanFields without a default value, & warns the user that the default has changed from False to Null. """ fields = [] for cls in models.get_models(): opts = cls._meta for f in opts.local_fields: if isinstance(f, models.BooleanField) and not f.has_default(): fields.append( '%s.%s: "%s"' % (opts.app_label, opts.object_name, f.name) ) if fields: fieldnames = ", ".join(fields) message = [ "You have not set a default value for one or more BooleanFields:", "%s." % fieldnames, "In Django 1.6 the default value of BooleanField was changed from", "False to Null when Field.default isn't defined. See", "https://docs.djangoproject.com/en/1.6/ref/models/fields/#booleanfield" "for more information." ] return ' '.join(message)
def display_for_field(value, field): from django.contrib.admin.templatetags.admin_list import _boolean_icon from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE if field.flatchoices: return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return _boolean_icon(value) elif value is None: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateTimeField): return formats.localize(timezone.template_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, models.FloatField): return formats.number_format(value) else: return smart_text(value)
def test_custom_migration_operation(self): project_state = self.set_up_test_model() operation = AddField( app_label='tests', model_name='pony', name='yellow', field=models.BooleanField(default=True) ) new_state = project_state.clone() operation.state_forwards('tests', new_state) self.assertColumnNotExists('tests_pony', 'yellow') with connection.schema_editor() as editor: operation.database_forwards('tests', editor, project_state, new_state) self.assertColumnExists('tests_pony', 'yellow') with connection.schema_editor() as editor: operation.database_backwards('tests', editor, new_state, project_state) self.assertColumnNotExists('tests_pony', 'yellow')