Skip to content

Commit 996e4d2

Browse files
committed
add django datetimefield page
1 parent cd958e0 commit 996e4d2

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ open source under the
148148
* [django.db.models Model](/django-db-models-model-examples.html)
149149
* [django.db.models BooleanField](/django-db-models-booleanfield-examples.html)
150150
* [django.db.models CharField](/django-db-models-charfield-examples.html)
151-
151+
* [django.db.models DateTimeField](/django-db-models-datetimefield-examples.html)
152152

153153
### django-oscar
154154
[django-oscar](https://github.com/django-oscar/django-oscar/)
@@ -177,6 +177,22 @@ Code examples from django-smithy are shown on the following pages:
177177
* [django.db.models TextField](/django-db-models-textfield-examples.html)
178178

179179

180+
### drf-action-serializer
181+
[drf-action-serializer](https://github.com/gregschmit/drf-action-serializer)
182+
is an extension for [Django REST Framework](/django-rest-framework-drf.html)
183+
that makes it easier to configure specific serializers to use based on the
184+
client's request action. For example, a list view should have one serializer
185+
whereas the detail view would have a different serializer.
186+
187+
The project is open source under the
188+
[MIT license](https://github.com/gregschmit/drf-action-serializer/blob/master/LICENSE).
189+
190+
There are code examples from the drf-action-serializer project on the
191+
following pages:
192+
193+
* [django.urls.path](/django-urls-path-examples.html)
194+
195+
180196
### gadget-board
181197
[gadget-board](https://github.com/mik4el/gadget-board) is a
182198
[Django](/django.html),
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
title: django.db.models DateTimeField Example Code
2+
category: page
3+
slug: django-db-models-datetimefield-examples
4+
sortorder: 50105
5+
toc: False
6+
sidebartitle: django.db.models DateTimeField
7+
meta: Python code examples for the DateTimeField class provided by the Django ORM. DateTimeField is found within the django.db.models module.
8+
9+
10+
[DateTimeField](https://github.com/django/django/blob/master/django/db/models/fields/__init__.py)
11+
is a frequently-used attribute on
12+
[Model](/django-db-models-model-examples.html) classes when defining
13+
date- and time-based [database columns](/databases.html) with
14+
the [Django ORM](/django-orm.html).
15+
16+
The [Django](/django.html) project has great documentation for
17+
[DateTimeField](https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.DateTimeField)
18+
and all of the other column fields.
19+
20+
Note that `DateTimeField` is defined within the
21+
[django.db.models.fields](https://github.com/django/django/blob/master/django/db/models/fields/__init__.py)
22+
module but is typically referenced from
23+
[django.db.models](https://github.com/django/django/tree/master/django/db/models)
24+
rather than including the `fields` module reference.
25+
26+
27+
## Example 1 from django-push-notifications
28+
[django-push-notifications](https://github.com/jazzband/django-push-notifications)
29+
is a [Django](/django.html) app for storing and interacting with
30+
push notification services such as
31+
[Google's Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/)
32+
and
33+
[Apple Notifications](https://developer.apple.com/notifications/).
34+
The django-push-notification project's source code is available
35+
open source under the
36+
[MIT license](https://github.com/jazzband/django-push-notifications/blob/master/LICENSE).
37+
38+
[**django-push-notifications / push_notifications / models.py**](
39+
40+
41+
```python
42+
from __future__ import unicode_literals
43+
44+
~~from django.db import models
45+
from django.utils.encoding import python_2_unicode_compatible
46+
from django.utils.translation import ugettext_lazy as _
47+
48+
from .fields import HexIntegerField
49+
from .settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS
50+
51+
52+
CLOUD_MESSAGE_TYPES = (
53+
("FCM", "Firebase Cloud Message"),
54+
("GCM", "Google Cloud Message"),
55+
)
56+
57+
BROWSER_TYPES = (
58+
("CHROME", "Chrome"),
59+
("FIREFOX", "Firefox"),
60+
("OPERA", "Opera"),
61+
)
62+
63+
64+
@python_2_unicode_compatible
65+
class Device(models.Model):
66+
name = models.CharField(max_length=255, verbose_name=_("Name"),
67+
blank=True, null=True)
68+
active = models.BooleanField(
69+
verbose_name=_("Is active"), default=True,
70+
help_text=_("Inactive devices will not be sent notifications")
71+
)
72+
user = models.ForeignKey(
73+
SETTINGS["USER_MODEL"], blank=True, null=True,
74+
on_delete=models.CASCADE
75+
)
76+
~~ date_created = models.DateTimeField(
77+
~~ verbose_name=_("Creation date"), auto_now_add=True, null=True
78+
~~ )
79+
application_id = models.CharField(
80+
max_length=64, verbose_name=_("Application ID"),
81+
help_text=_(
82+
"Opaque application identity, should be filled in for"
83+
" multiple key/certificate access"
84+
),
85+
blank=True, null=True
86+
)
87+
88+
class Meta:
89+
abstract = True
90+
91+
def __str__(self):
92+
return (
93+
self.name or
94+
str(self.device_id or "") or
95+
"%s for %s" % (self.__class__.__name__,
96+
self.user or "unknown user")
97+
)
98+
99+
## source code continues here without further DateTimeField examples
100+
```

content/pages/examples/django/django-urls-path.markdown

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ backend that displays
6363
code is open source under the
6464
[MIT license](https://github.com/Michael-Cantley/heritagesites/blob/master/LICENSE).
6565

66-
[**heritagesites/heritagesites/urls.py**](https://github.com/Michael-Cantley/heritagesites/blob/master/heritagesites/urls.py)
66+
[**heritagesites / heritagesites / urls.py**](https://github.com/Michael-Cantley/heritagesites/blob/master/heritagesites/urls.py)
6767

6868
```python
6969
# urls.py
@@ -87,3 +87,34 @@ urlpatterns = [
8787
~~ path('sites/search', views.SiteFilterView.as_view(), name="search")
8888
]
8989
```
90+
91+
## Example 3 from drf-action-serializer
92+
[drf-action-serializer](https://github.com/gregschmit/drf-action-serializer)
93+
is an extension for [Django REST Framework](/django-rest-framework-drf.html)
94+
that makes it easier to configure specific serializers to use based on the
95+
client's request action. For example, a list view should have one serializer
96+
whereas the detail view would have a different serializer.
97+
98+
The project is open source under the
99+
[MIT license](https://github.com/gregschmit/drf-action-serializer/blob/master/LICENSE).
100+
101+
[**drf-action-serializer / action_serializer / urls.py**](https://github.com/gregschmit/drf-action-serializer/blob/master/action_serializer/urls.py)
102+
103+
```python
104+
from django.contrib import admin
105+
~~from django.urls import include, path
106+
from rest_framework.routers import DefaultRouter
107+
108+
from .sample_group_viewset import GroupViewSet
109+
110+
111+
router = DefaultRouter()
112+
router.register('auth/group', GroupViewSet)
113+
router.register('auth/groups', GroupViewSet)
114+
115+
urlpatterns = [
116+
~~ path('api/', include(router.urls)),
117+
~~ path('admin/doc/', include('django.contrib.admindocs.urls')),
118+
~~ path('admin/', admin.site.urls),
119+
]
120+
```

theme/templates/table-of-contents.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ <h4 class="bp"><a href="/django-core-mail-messages-emailmessage-examples.html">d
240240
<h4 class="bp"><a href="/django-core-management-base-basecommand-examples.html">django.core.management.base BaseCommand</a></h4>
241241
<h4 class="bp"><a href="/django-db-models-booleanfield-examples.html">django.db.models BooleanField</a></h4>
242242
<h4 class="bp"><a href="/django-db-models-charfield-examples.html">django.db.models CharField</a></h4>
243+
<h4 class="bp"><a href="/django-db-models-datetimefield-examples.html">django.db.models DateTimeField</a></h4>
243244
<h4 class="bp"><a href="/django-db-models-model-examples.html">django.db.models Model</a></h4>
244245
<h4 class="bp"><a href="/django-db-models-textfield-examples.html">django.db.models TextField</a></h4>
245246
<h4 class="bp"><a href="/django-db-models-signal-examples.html">django.db.models.signal</a></h4>

0 commit comments

Comments
 (0)