views:

443

answers:

2

I have a django model called Blog.

I'd like to add a field to my current model that is for last_modified_date. I know how to set a default value, but I would like somehow for it to get automatically updated anytime I modify the blog entry via the admin interface.

Is there some way to force this value to the current time on each admin site save?

Also would there be some way to add a mod_count field and have it automatically calculated on each modify of the admin site blog entry?

+2  A: 

Create a datetime field in your model. Have it update whenever it is saved. This requires you to use the auto_now_add option:

class DateTimeField([auto_now=False, auto_now_add=False, **options])

DateTimeField.auto_now_add¶

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.

It should look something like this:

class Message(models.Model):
    message = models.TextField()
    active = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)

Model field reference

For the second part, I think you have to overload

ModelAdmin.save_model(self, request, obj, form, change)

As James Bennett describes here. It will look something like this:

class EntryAdmin(admin.ModelAdmin):

def save_model(self, request, obj, form, change):
    if change:
        obj.change_count += 1
    obj.save()
hughdbrown
Thanks, do you know of a way to auto increment a number by 1 on each save?
Net Citizen
Yeah, I think you have to overload the Model.save() to do the increment during the save. James Bennett has an article on this at b-list.org, as I recall. His article had to do with saving the current user on a record in the admin interface. I haven't found the article yet....
hughdbrown
+2  A: 

There's a number of ways you can increase the edit count each time it's saved.

The model itself has a save() method, and the admin model has a model_save() method.

So for example, let's say you wanted it to increment when it was edited with the admin tool....

models.py:

class MyModel(models.Model):
    edit_count = models.IntegerField()
    # ... rest of model code here...


admin.py:

class MyModelAdmin(admin.ModelAdmin)
    def save_model(self, request, obj, form, change):
        if not change:
            obj.edit_count = 1
        else:
            obj.edit_count += 1
        obj.save()

You could do similar code off of the model save() event as well.


Something else you may be interested in is django-command-extensions. It adds 2 fields which may be helpful to you:

  • CreationDateTimeField - DateTimeField that will automatically set it's date when the object is first saved to the database.

  • ModificationDateTimeField - DateTimeField that will automatically set it's date when an object is saved to the database.

T. Stone
Can you point me at the documentation that says auto_now_add is deprecated? As recently as 2008-06-15, this assertion was denied: "I don't know who anonymous is, but auto_now_add hasn't been deprecated yet. There has been talk about deprecating, and there is a better way (default=datetime.now), but for the moment, auto_now_add is still a valid option. " http://code.djangoproject.com/ticket/7390
hughdbrown
I copied the field description off of the django-command-extensions app project page. You'd have to talk with them about that assertion.
T. Stone
To not propagate mis-information *just in case*, I've removed that from my pasted text.
T. Stone