Skip to content

Commit cd958e0

Browse files
committed
add new django charfield example
1 parent 350a8fe commit cd958e0

File tree

4 files changed

+179
-1
lines changed

4 files changed

+179
-1
lines changed

content/pages/examples/django/django-code-examples.markdown

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,26 @@ in Django's admin interface. The project's code is available under the
130130
Code from django-filer can be found on these pages:
131131

132132
* [django.conf settings](/django-conf-settings-examples.html)
133-
* [django.contrib.admin](/django-contrib-admin.html)
133+
* [django.contrib.admin](/django-contrib-admin-examples.html)
134134
* [django.core.management.base BaseCommand](/django-core-management-base-basecommand-examples.html)
135135

136136

137+
### django-push-notifications
138+
[django-push-notifications](https://github.com/jazzband/django-push-notifications)
139+
is a [Django](/django.html) app for storing and interacting with
140+
push notification services such as
141+
[Google's Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/)
142+
and
143+
[Apple Notifications](https://developer.apple.com/notifications/).
144+
The django-push-notification project's source code is available
145+
open source under the
146+
[MIT license](https://github.com/jazzband/django-push-notifications/blob/master/LICENSE).
147+
148+
* [django.db.models Model](/django-db-models-model-examples.html)
149+
* [django.db.models BooleanField](/django-db-models-booleanfield-examples.html)
150+
* [django.db.models CharField](/django-db-models-charfield-examples.html)
151+
152+
137153
### django-oscar
138154
[django-oscar](https://github.com/django-oscar/django-oscar/)
139155
([project website](http://oscarcommerce.com/))
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
```

content/pages/examples/django/django-db-models-charfield.markdown

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,77 @@ class Request(TimeStampedModel):
105105

106106
## source file continues from here without further CharField examples
107107
```
108+
109+
### Example 2 from django-push-notifications
110+
[django-push-notifications](https://github.com/jazzband/django-push-notifications)
111+
is a [Django](/django.html) app for storing and interacting with
112+
push notification services such as
113+
[Google's Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/)
114+
and
115+
[Apple Notifications](https://developer.apple.com/notifications/).
116+
The django-push-notification project's source code is available
117+
open source under the
118+
[MIT license](https://github.com/jazzband/django-push-notifications/blob/master/LICENSE).
119+
120+
[**django-push-notifications / push_notifications / models.py**](https://github.com/jazzband/django-push-notifications/blob/master/push_notifications/models.py)
121+
122+
```python
123+
from __future__ import unicode_literals
124+
125+
~~from django.db import models
126+
from django.utils.encoding import python_2_unicode_compatible
127+
from django.utils.translation import ugettext_lazy as _
128+
129+
from .fields import HexIntegerField
130+
from .settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS
131+
132+
133+
CLOUD_MESSAGE_TYPES = (
134+
("FCM", "Firebase Cloud Message"),
135+
("GCM", "Google Cloud Message"),
136+
)
137+
138+
BROWSER_TYPES = (
139+
("CHROME", "Chrome"),
140+
("FIREFOX", "Firefox"),
141+
("OPERA", "Opera"),
142+
)
143+
144+
145+
@python_2_unicode_compatible
146+
class Device(models.Model):
147+
~~ name = models.CharField(max_length=255, verbose_name=_("Name"),
148+
~~ blank=True, null=True)
149+
active = models.BooleanField(
150+
verbose_name=_("Is active"), default=True,
151+
help_text=_("Inactive devices will not be sent notifications")
152+
)
153+
user = models.ForeignKey(
154+
SETTINGS["USER_MODEL"], blank=True, null=True,
155+
on_delete=models.CASCADE
156+
)
157+
date_created = models.DateTimeField(
158+
verbose_name=_("Creation date"), auto_now_add=True, null=True
159+
)
160+
~~ application_id = models.CharField(
161+
~~ max_length=64, verbose_name=_("Application ID"),
162+
~~ help_text=_(
163+
~~ "Opaque application identity, should be filled in for"
164+
~~ " multiple key/certificate access"
165+
~~ ),
166+
~~ blank=True, null=True
167+
~~ )
168+
169+
class Meta:
170+
abstract = True
171+
172+
def __str__(self):
173+
return (
174+
self.name or
175+
str(self.device_id or "") or
176+
"%s for %s" % (self.__class__.__name__,
177+
self.user or "unknown user")
178+
)
179+
180+
## source file continues from here with a few other simple CharField examples
181+
```

theme/templates/table-of-contents.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ <h4 class="bp"><a href="/django-contrib-admin-filters-simplelistfilter-examples.
238238
<h4 class="bp"><a href="/django-core-mail-send-mail-examples.html">django.core.mail.send_mail</a></h4>
239239
<h4 class="bp"><a href="/django-core-mail-messages-emailmessage-examples.html">django.core.mail.messages EmailMessage</a></h4>
240240
<h4 class="bp"><a href="/django-core-management-base-basecommand-examples.html">django.core.management.base BaseCommand</a></h4>
241+
<h4 class="bp"><a href="/django-db-models-booleanfield-examples.html">django.db.models BooleanField</a></h4>
241242
<h4 class="bp"><a href="/django-db-models-charfield-examples.html">django.db.models CharField</a></h4>
242243
<h4 class="bp"><a href="/django-db-models-model-examples.html">django.db.models Model</a></h4>
243244
<h4 class="bp"><a href="/django-db-models-textfield-examples.html">django.db.models TextField</a></h4>

0 commit comments

Comments
 (0)