Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Avoided mixing dates and datetimes in the examples.

Refs #16023.
  • Loading branch information...
commit e69348b4e7f07ef927edaecc7126901fc91c79d0 1 parent b7d3b05
Aymeric Augustin authored September 08, 2012
2  docs/faq/models.txt
@@ -11,7 +11,7 @@ Then, just do this::
11 11
 
12 12
     >>> from django.db import connection
13 13
     >>> connection.queries
14  
-    [{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls',
  14
+    [{'sql': 'SELECT polls_polls.id, polls_polls.question, polls_polls.pub_date FROM polls_polls',
15 15
     'time': '0.002'}]
16 16
 
17 17
 ``connection.queries`` is only available if :setting:`DEBUG` is ``True``.
8  docs/intro/overview.txt
@@ -31,7 +31,7 @@ the file ``mysite/news/models.py``::
31 31
             return self.full_name
32 32
 
33 33
     class Article(models.Model):
34  
-        pub_date = models.DateTimeField()
  34
+        pub_date = models.DateField()
35 35
         headline = models.CharField(max_length=200)
36 36
         content = models.TextField()
37 37
         reporter = models.ForeignKey(Reporter)
@@ -96,8 +96,8 @@ access your data. The API is created on the fly, no code generation necessary::
96 96
     DoesNotExist: Reporter matching query does not exist. Lookup parameters were {'id': 2}
97 97
 
98 98
     # Create an article.
99  
-    >>> from datetime import datetime
100  
-    >>> a = Article(pub_date=datetime.now(), headline='Django is cool',
  99
+    >>> from datetime import date
  100
+    >>> a = Article(pub_date=date.today(), headline='Django is cool',
101 101
     ...     content='Yeah.', reporter=r)
102 102
     >>> a.save()
103 103
 
@@ -140,7 +140,7 @@ as registering your model in the admin site::
140 140
     from django.db import models
141 141
 
142 142
     class Article(models.Model):
143  
-        pub_date = models.DateTimeField()
  143
+        pub_date = models.DateField()
144 144
         headline = models.CharField(max_length=200)
145 145
         content = models.TextField()
146 146
         reporter = models.ForeignKey(Reporter)
8  docs/ref/contrib/comments/moderation.txt
@@ -32,11 +32,11 @@ A simple example is the best illustration of this. Suppose we have the
32 32
 following model, which would represent entries in a Weblog::
33 33
 
34 34
     from django.db import models
35  
-    
  35
+
36 36
     class Entry(models.Model):
37 37
         title = models.CharField(maxlength=250)
38 38
         body = models.TextField()
39  
-        pub_date = models.DateTimeField()
  39
+        pub_date = models.DateField()
40 40
         enable_comments = models.BooleanField()
41 41
 
42 42
 Now, suppose that we want the following steps to be applied whenever a
@@ -55,11 +55,11 @@ Accomplishing this is fairly straightforward and requires very little
55 55
 code::
56 56
 
57 57
     from django.contrib.comments.moderation import CommentModerator, moderator
58  
-    
  58
+
59 59
     class EntryModerator(CommentModerator):
60 60
         email_notification = True
61 61
         enable_field = 'enable_comments'
62  
-    
  62
+
63 63
     moderator.register(Entry, EntryModerator)
64 64
 
65 65
 The :class:`CommentModerator` class pre-defines a number of useful moderation
4  docs/ref/models/fields.txt
@@ -983,10 +983,10 @@ define the details of how the relation works.
983 983
     this with functions from the Python ``datetime`` module to limit choices of
984 984
     objects by date. For example::
985 985
 
986  
-        limit_choices_to = {'pub_date__lte': datetime.now}
  986
+        limit_choices_to = {'pub_date__lte': datetime.date.today}
987 987
 
988 988
     only allows the choice of related objects with a ``pub_date`` before the
989  
-    current date/time to be chosen.
  989
+    current date to be chosen.
990 990
 
991 991
     Instead of a dictionary this can also be a :class:`~django.db.models.Q`
992 992
     object for more :ref:`complex queries <complex-lookups-with-q>`. However,
2  docs/ref/models/instances.txt
@@ -135,7 +135,7 @@ access to more than a single field::
135 135
             raise ValidationError('Draft entries may not have a publication date.')
136 136
         # Set the pub_date for published items if it hasn't been set already.
137 137
         if self.status == 'published' and self.pub_date is None:
138  
-            self.pub_date = datetime.datetime.now()
  138
+            self.pub_date = datetime.date.today()
139 139
 
140 140
 Any :exc:`~django.core.exceptions.ValidationError` exceptions raised by
141 141
 ``Model.clean()`` will be stored in a special key error dictionary key,
13  docs/ref/models/querysets.txt
@@ -1945,6 +1945,17 @@ SQL equivalent::
1945 1945
 You can use ``range`` anywhere you can use ``BETWEEN`` in SQL — for dates,
1946 1946
 numbers and even characters.
1947 1947
 
  1948
+.. warning::
  1949
+
  1950
+    Filtering a ``DateTimeField`` with dates won't include items on the last
  1951
+    day, because the bounds are interpreted as "0am on the given date". If
  1952
+    ``pub_date`` was a ``DateTimeField``, the above expression would be turned
  1953
+    into this SQL::
  1954
+
  1955
+        SELECT ... WHERE pub_date BETWEEN '2005-01-01 00:00:00' and '2005-03-31 00:00:00';
  1956
+
  1957
+    Generally speaking, you can't mix dates and datetimes.
  1958
+
1948 1959
 .. fieldlookup:: year
1949 1960
 
1950 1961
 year
@@ -1958,7 +1969,7 @@ Example::
1958 1969
 
1959 1970
 SQL equivalent::
1960 1971
 
1961  
-    SELECT ... WHERE pub_date BETWEEN '2005-01-01' AND '2005-12-31 23:59:59.999999';
  1972
+    SELECT ... WHERE pub_date BETWEEN '2005-01-01' AND '2005-12-31';
1962 1973
 
1963 1974
 (The exact SQL syntax varies for each database engine.)
1964 1975
 
8  docs/topics/db/examples/many_to_one.txt
@@ -42,8 +42,8 @@ Create a few Reporters::
42 42
 
43 43
 Create an Article::
44 44
 
45  
-    >>> from datetime import datetime
46  
-    >>> a = Article(id=None, headline="This is a test", pub_date=datetime(2005, 7, 27), reporter=r)
  45
+    >>> from datetime import date
  46
+    >>> a = Article(id=None, headline="This is a test", pub_date=date(2005, 7, 27), reporter=r)
47 47
     >>> a.save()
48 48
 
49 49
     >>> a.reporter.id
@@ -65,7 +65,7 @@ database, which always returns unicode strings)::
65 65
 
66 66
 Create an Article via the Reporter object::
67 67
 
68  
-    >>> new_article = r.article_set.create(headline="John's second story", pub_date=datetime(2005, 7, 29))
  68
+    >>> new_article = r.article_set.create(headline="John's second story", pub_date=date(2005, 7, 29))
69 69
     >>> new_article
70 70
     <Article: John's second story>
71 71
     >>> new_article.reporter
@@ -75,7 +75,7 @@ Create an Article via the Reporter object::
75 75
 
76 76
 Create a new article, and add it to the article set::
77 77
 
78  
-    >>> new_article2 = Article(headline="Paul's story", pub_date=datetime(2006, 1, 17))
  78
+    >>> new_article2 = Article(headline="Paul's story", pub_date=date(2006, 1, 17))
79 79
     >>> r.article_set.add(new_article2)
80 80
     >>> new_article2.reporter
81 81
     <Reporter: John Smith>
12  docs/topics/db/queries.txt
@@ -35,8 +35,8 @@ models, which comprise a Weblog application:
35 35
         blog = models.ForeignKey(Blog)
36 36
         headline = models.CharField(max_length=255)
37 37
         body_text = models.TextField()
38  
-        pub_date = models.DateTimeField()
39  
-        mod_date = models.DateTimeField()
  38
+        pub_date = models.DateField()
  39
+        mod_date = models.DateField()
40 40
         authors = models.ManyToManyField(Author)
41 41
         n_comments = models.IntegerField()
42 42
         n_pingbacks = models.IntegerField()
@@ -233,7 +233,7 @@ refinements together. For example::
233 233
     >>> Entry.objects.filter(
234 234
     ...     headline__startswith='What'
235 235
     ... ).exclude(
236  
-    ...     pub_date__gte=datetime.now()
  236
+    ...     pub_date__gte=datetime.date.today()
237 237
     ... ).filter(
238 238
     ...     pub_date__gte=datetime(2005, 1, 30)
239 239
     ... )
@@ -258,8 +258,8 @@ stored, used and reused.
258 258
 Example::
259 259
 
260 260
     >> q1 = Entry.objects.filter(headline__startswith="What")
261  
-    >> q2 = q1.exclude(pub_date__gte=datetime.now())
262  
-    >> q3 = q1.filter(pub_date__gte=datetime.now())
  261
+    >> q2 = q1.exclude(pub_date__gte=datetime.date.today())
  262
+    >> q3 = q1.filter(pub_date__gte=datetime.date.today())
263 263
 
264 264
 These three ``QuerySets`` are separate. The first is a base
265 265
 :class:`~django.db.models.query.QuerySet` containing all entries that contain a
@@ -282,7 +282,7 @@ actually run the query until the :class:`~django.db.models.query.QuerySet` is
282 282
 *evaluated*. Take a look at this example::
283 283
 
284 284
     >>> q = Entry.objects.filter(headline__startswith="What")
285  
-    >>> q = q.filter(pub_date__lte=datetime.now())
  285
+    >>> q = q.filter(pub_date__lte=datetime.date.today())
286 286
     >>> q = q.exclude(body_text__icontains="food")
287 287
     >>> print(q)
288 288
 

0 notes on commit e69348b

Please sign in to comment.
Something went wrong with that request. Please try again.