Login

Snippets by lindsayrgwatt

Snippet List

Timestamps in Model

A simple way to add `date_created` and `date_modified` timestamps to a model. Adds a `date_created` timestamp when the object is first created and adds a `date_modified` timestamp whenever the item is saved. **Note:** You might be tempted instead to use: `date_created=models.DateTimeField(default=datetime.now())` but that won't work as Python will calculate `datetime.now()` only once when it interprets your model. This means that every object created will get the same `date_created` timestamp until you restart your server.

  • python
  • save
  • timestamp
Read More

lindsayrgwatt has posted 1 snippet.