0

I'm toying around in django trying to understand its philosophy, and I tried to create a last modified entry. I place this in my code:

slide_title = models.CharField(max_length=200)
last_date = models.DateTimeField('last modified',auto_now=True)
def __str__(self):
    out = 'title: {}\n last modified: {}'.format(
            self.slide_title,
            type(self.last_date)
            )
    return out

But when I start a shell (using python manage.py shell) I get the following

In [2]: from pressent.models import Slide

In [3]: Slide.objects.all()
Out[3]: 
[<Slide: title: title
last modified: <type 'NoneType'>>]

Why isn't it a DateTimeField?

Yotam
  • 10,295
  • 30
  • 88
  • 128
  • How did you create the object? Try calling `slide.save()` on it. `auto_now` should set the current datetime when the object's save method is called. – Håken Lid Jun 11 '16 at 16:32

1 Answers1

1

You did an auto_now=True. But that works only when Model.save() method is called. To populate the DateTimeField at the time of creation, you need to add another attribute auto_now_add=True.

Reference

Tejas Jadhav
  • 93
  • 2
  • 7
  • I'm not sure I understood you correctly, is this undoable without saving the ``Slide`` or should adding the field ``auto_now_add=True`` should solve this? At any rate, I updated my code, and saved a new slide, with the same result. – Yotam Jun 12 '16 at 13:03
  • Your existing rows will not change. To see the new changes, add a new entry in `Slide` model. The `DateTimeField` should auto-populate. – Tejas Jadhav Jun 12 '16 at 16:04