Code

Changes between Version 40 and Version 41 of IrcFAQ


Ignore:
Timestamp:
07/17/07 23:13:35 (7 years ago)
Author:
ubernostrum
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • IrcFAQ

    v40 v41  
    133133 
    134134Both mod_python and FastCGI are structured in such a way that there's no such thing as "application startup" or "server startup"; the best solution is to place your "startup" code somewhere that's guaranteed to be imported early on in the request/response cycle (the `__init__.py` file of your project, or of a specific application you're using, can be a good place, because Python will execute code found there the first time it has to import the module; just be aware that referencing the same module in different ways, say by doing `from myproject.myapp import foo` in one place, and `from myapp import foo` in another, will cause that code to be executed once for each different way you import it). 
     135 
     136== I have a `DateField` or `DateTimeField` with a default value of `now()`, but it's not working! == 
     137 
     138What you've probably done is something like this: 
     139 
     140{{{ 
     141some_date_field = models.DateTimeField(default=datetime.datetime.now()) 
     142}}} 
     143 
     144When you do that you're immediately calling `datetime.datetime.now` and passing its return value -- ''at the moment the class is defined'' -- to be the default, which means it will not change during the life of a server process. What you want to do instead is this: 
     145 
     146{{{ 
     147some_date_field = models.DateTimeField(default=datetime.datetime.now) 
     148}}} 
     149 
     150Note that there are no parentheses on `datetime.datetime.now` in this version, so you're passing ''the function itself'' to be the default. When Django receives a function as a default value for a model field, it will call the function each time a new object is saved, and that will correctly generate the current date/time for each object.