Reputation: 1186
I am making a blog with django. I have an entry class, with title, content, publish date, etc.
The date field should save the date when the entry was created. But it changes if I edit the entry(If i changes the content or the title)
This is the code of the model:
class Entry(models.Model):
title = models.CharField(max_length=70)
content = models.TextField()
pub_date = models.DateTimeField(auto_now=True)
image = models.TextField()
tags = models.ManyToManyField(Tag)
def __unicode__(self):
return self.title
class Admin:
pass
class Meta:
ordering = ['-pub_date',]
Upvotes: 0
Views: 465
Reputation: 6756
Change auto_now
to auto_now_add
.
From django docs:
DateField.auto_now
Automatically set the field to now every time the object is saved. Useful for "last-modified" timestamps. Note that the current date is always used; it's not just a default value that you can override.
DateField.auto_now_add
Automatically set the field to now when the object is first created. Useful for creation of timestamps. Note that the current date is always used; it's not just a default value that you can override.
Upvotes: 1