Nico Müller
Nico Müller

Reputation: 1874

Django model datetimefield updates but not in db

I have the following in my Memo model

times_visited = models.IntegerField(default=0)
date_last_visited = models.DateTimeField(default=timezone.now())

When calling as below, only times_visited int is being updated in the database correctly, the date_visited stays the same as during initialisation. The print debug shows the correct time is stored in the model, but it seems it doesnt get pushed to the db. I did not override the save method.

memo.times_visited = memo.times_visited + 1
memo.last_visited = timezone.now()
memo.save()

print("Last visited: " + str(memo.last_visited))

Should this not be working this way?

Upvotes: 0

Views: 43

Answers (1)

Yugandhar Chaudhari
Yugandhar Chaudhari

Reputation: 3964

It should be

memo.date_last_visited = timezone.now()

not

memo.last_visited = timezone.now()

As dirkgroten said you are assigning last_visited property to memo object

Upvotes: 1

Related Questions