|
| 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 | +``` |
0 commit comments