Python django.db.models.fields.BooleanField() Examples

The following are 14 code examples of django.db.models.fields.BooleanField(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.db.models.fields , or try the search function .
Example #1
Source File: djpeewee.py    From Quiver-alfred with MIT License 6 votes vote down vote up
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),
        ] 
Example #2
Source File: bigquery.py    From openprescribing with MIT License 6 votes vote down vote up
def build_schema_from_model(model):
    field_mappings = {
        model_fields.BigIntegerField: "INTEGER",
        model_fields.CharField: "STRING",
        model_fields.DateField: "DATE",
        model_fields.FloatField: "FLOAT",
        model_fields.DecimalField: "NUMERIC",
        model_fields.IntegerField: "INTEGER",
        model_fields.BooleanField: "BOOLEAN",
        model_fields.NullBooleanField: "BOOLEAN",
        model_fields.TextField: "STRING",
        related_fields.ForeignKey: "INTEGER",
        related_fields.OneToOneField: "INTEGER",
    }

    fields = [
        (f.name, field_mappings[type(f)])
        for f in model._meta.fields
        if not f.auto_created
    ]

    return build_schema(*fields) 
Example #3
Source File: build_search_filters.py    From openprescribing with MIT License 6 votes vote down vote up
def _build_search_filter(cls, field_name):
    if field_name == "bnf_code":
        return _build_search_filter_bnf_code_prefox()

    field = cls._meta.get_field(field_name)
    builder = {
        ForeignKey: _build_search_filter_fk,
        ManyToOneRel: _build_search_filter_rev_fk,
        OneToOneRel: _build_search_filter_rev_fk,
        fields.CharField: _build_search_filter_char,
        fields.DateField: _build_search_filter_date,
        fields.BooleanField: _build_search_filter_boolean,
        fields.DecimalField: _build_search_filter_decimal,
    }[type(field)]
    search_filter = builder(field)
    search_filter["id"] = field_name
    return search_filter 
Example #4
Source File: test_fields.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def test_builtin_fields(self):
        self.assertEqual(
            views.get_readable_field_data_type(fields.BooleanField()),
            _('Boolean (Either True or False)')
        ) 
Example #5
Source File: mixins.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_json_data(self, **kwargs):
        qs = kwargs.get('qs')
        if qs is None:
            qs = self.get_queryset()
        extra_json_fields = list(self.extra_json_fields)
        extra_json_fields.append('pk')
        extra_json_fields += list(self.annotate.keys())
        if not self.json_fields:
            self.json_fields = [f.name for f in self.model._meta.concrete_fields]

        if 'columns' in self.request.GET:
            columns = self.request.GET['columns'].split(',')
        else:
            columns = self.json_fields + extra_json_fields
        data = list(qs.annotate(**self.annotate).values(*columns))

        # TODO: consider replace none_to_bool - either use default=False or update jqWidgets
        bool_fields = [i.name for i in self.model._meta.fields
                       if isinstance(i, django_fields.BooleanField)]
        for row in data:
            row.update((k, False) for k, v in row.items() if v is None and k in bool_fields)
            if not kwargs.get('keep_tags'):
                row.update((k, v.replace("<", "&lt;").replace(">", "&gt;"))
                           for k, v in row.items() if isinstance(v, str))

        return data 
Example #6
Source File: expressions.py    From python with Apache License 2.0 5 votes vote down vote up
def output_field(self):
        return fields.BooleanField() 
Example #7
Source File: expressions.py    From python2017 with MIT License 5 votes vote down vote up
def output_field(self):
        return fields.BooleanField() 
Example #8
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_boolean_constraints(self):
        """Boolean fields have check constraints on their values."""
        for field in (BooleanField(), NullBooleanField(), BooleanField(null=True)):
            with self.subTest(field=field):
                field.set_attributes_from_name('is_nice')
                self.assertIn('"IS_NICE" IN (0,1)', field.db_check(connection)) 
Example #9
Source File: test_views.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_builtin_fields(self):
        self.assertEqual(
            views.get_readable_field_data_type(fields.BooleanField()),
            'Boolean (Either True or False)'
        ) 
Example #10
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_boolean_constraints(self):
        """Boolean fields have check constraints on their values."""
        for field in (BooleanField(), NullBooleanField(), BooleanField(null=True)):
            with self.subTest(field=field):
                field.set_attributes_from_name('is_nice')
                self.assertIn('"IS_NICE" IN (0,1)', field.db_check(connection)) 
Example #11
Source File: test_views.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_builtin_fields(self):
        self.assertEqual(
            views.get_readable_field_data_type(fields.BooleanField()),
            'Boolean (Either True or False)'
        ) 
Example #12
Source File: build_search_query.py    From openprescribing with MIT License 5 votes vote down vote up
def _build_lookup_key(cls, field_name, operator):
    field = cls._meta.get_field(field_name)
    builder = {
        ForeignKey: _build_lookup_fk,
        ManyToOneRel: _build_lookup_rev_fk,
        OneToOneRel: _build_lookup_rev_fk,
        fields.CharField: _build_lookup_char,
        fields.DateField: _build_lookup_date,
        fields.BooleanField: _build_lookup_boolean,
        fields.DecimalField: _build_lookup_decimal,
    }[type(field)]
    return builder(cls, field_name, operator) 
Example #13
Source File: views.py    From openprescribing with MIT License 5 votes vote down vote up
def _build_row(obj, field):
    value = getattr(obj, field.name)
    if value is None:
        return

    if field.name == "invalid" and not value:
        return

    if isinstance(field, ForeignKey):
        related_model = field.related_model
        if related_model in cls_to_obj_type:
            obj_type = cls_to_obj_type[related_model]
            link = reverse("dmd_obj", args=[obj_type, value.id])
            text = getattr(value, related_model.name_field)
            return {
                "key": related_model._meta.verbose_name,
                "value": text,
                "link": link,
            }

        try:
            value = value.descr
        except AttributeError:
            value = value.nm

    elif isinstance(field, fields.BooleanField):
        value = {True: "✓", False: "✗"}.get(value)

    return {"key": field.help_text, "value": value} 
Example #14
Source File: import_dmd.py    From openprescribing with MIT License 4 votes vote down vote up
def import_model(self, model, elements):
        """Import model instances from list of XML elements."""

        model.objects.all().delete()

        boolean_field_names = [
            f.name
            for f in model._meta.fields
            if isinstance(f, django_fields.BooleanField)
        ]

        table_name = model._meta.db_table
        column_names = [
            f.db_column or f.name
            for f in model._meta.fields
            if not isinstance(f, django_fields.AutoField)
        ]
        sql = "INSERT INTO {} ({}) VALUES ({})".format(
            table_name, ", ".join(column_names), ", ".join(["%s"] * len(column_names))
        )

        values = []

        for element in elements:
            row = {}

            for field_element in element:
                name = field_element.tag.lower()
                if name == "desc":
                    # "desc" is a really unhelpful field name if you're writing
                    # SQL!
                    name = "descr"
                elif name == "dnd":
                    # For consistency with the rest of the data, we rename
                    # "dnd" to "dndcd", as it is a foreign key field.
                    name = "dndcd"

                value = field_element.text
                row[name] = value

            for name in boolean_field_names:
                row[name] = name in row

            values.append([row.get(name) for name in column_names])

        with connection.cursor() as cursor:
            cursor.executemany(sql, values)