|
| 1 | +title: django.db.models BooleanField Example Code |
| 2 | +category: page |
| 3 | +slug: django-db-models-booleanfield-examples |
| 4 | +sortorder: 50103 |
| 5 | +toc: False |
| 6 | +sidebartitle: django.db.models BooleanField |
| 7 | +meta: Python code examples for the BooleanField class used with the Django ORM to create a Boolean column in a database. |
| 8 | + |
| 9 | + |
| 10 | +[BooleanField](https://github.com/django/django/blob/master/django/db/models/fields/__init__.py) |
| 11 | +is a Python class within [Django](/django.html) that maps Python code to |
| 12 | +a [relational database](/databases.html) Boolean column through the |
| 13 | +[Django object-relational-mapper (ORM)](/django-orm.html). |
| 14 | + |
| 15 | +[Django](/django.html)'s documentation explains more about |
| 16 | +[BooleanField](https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.TextField) |
| 17 | +and all of the other ORM column fields. |
| 18 | + |
| 19 | +Note that `BooleanField` is defined within the |
| 20 | +[django.db.models.fields](https://github.com/django/django/blob/master/django/db/models/fields/__init__.py) |
| 21 | +module but is typically referenced from |
| 22 | +[django.db.models](https://github.com/django/django/tree/master/django/db/models) |
| 23 | +rather than including the `fields` module reference. |
| 24 | + |
| 25 | + |
| 26 | +## Example 1 from django-push-notifications |
| 27 | +[django-push-notifications](https://github.com/jazzband/django-push-notifications) |
| 28 | +is a [Django](/django.html) app for storing and interacting with |
| 29 | +push notification services such as |
| 30 | +[Google's Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/) |
| 31 | +and |
| 32 | +[Apple Notifications](https://developer.apple.com/notifications/). |
| 33 | +The django-push-notification project's source code is available |
| 34 | +open source under the |
| 35 | +[MIT license](https://github.com/jazzband/django-push-notifications/blob/master/LICENSE). |
| 36 | + |
| 37 | +[**django-push-notifications / push_notifications / models.py**](https://github.com/jazzband/django-push-notifications/blob/master/push_notifications/models.py) |
| 38 | + |
| 39 | +```python |
| 40 | +from __future__ import unicode_literals |
| 41 | + |
| 42 | +~~from django.db import models |
| 43 | +from django.utils.encoding import python_2_unicode_compatible |
| 44 | +from django.utils.translation import ugettext_lazy as _ |
| 45 | + |
| 46 | +from .fields import HexIntegerField |
| 47 | +from .settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS |
| 48 | + |
| 49 | + |
| 50 | +CLOUD_MESSAGE_TYPES = ( |
| 51 | + ("FCM", "Firebase Cloud Message"), |
| 52 | + ("GCM", "Google Cloud Message"), |
| 53 | +) |
| 54 | + |
| 55 | +BROWSER_TYPES = ( |
| 56 | + ("CHROME", "Chrome"), |
| 57 | + ("FIREFOX", "Firefox"), |
| 58 | + ("OPERA", "Opera"), |
| 59 | +) |
| 60 | + |
| 61 | + |
| 62 | +@python_2_unicode_compatible |
| 63 | +class Device(models.Model): |
| 64 | + name = models.CharField(max_length=255, verbose_name=_("Name"), |
| 65 | + blank=True, null=True) |
| 66 | +~~ active = models.BooleanField( |
| 67 | +~~ verbose_name=_("Is active"), default=True, |
| 68 | +~~ help_text=_("Inactive devices will not be sent notifications") |
| 69 | +~~ ) |
| 70 | + user = models.ForeignKey( |
| 71 | + SETTINGS["USER_MODEL"], blank=True, null=True, |
| 72 | + on_delete=models.CASCADE |
| 73 | + ) |
| 74 | + date_created = models.DateTimeField( |
| 75 | + verbose_name=_("Creation date"), auto_now_add=True, null=True |
| 76 | + ) |
| 77 | + application_id = models.CharField( |
| 78 | + max_length=64, verbose_name=_("Application ID"), |
| 79 | + help_text=_( |
| 80 | + "Opaque application identity, should be filled in for" |
| 81 | + " multiple key/certificate access" |
| 82 | + ), |
| 83 | + blank=True, null=True |
| 84 | + ) |
| 85 | + |
| 86 | +## source file continues here without further BooleanField examples |
| 87 | +``` |
0 commit comments