Re: What't the best way to track created by and modified by in Django 1.7?

330 views
Skip to first unread message

Russell Keith-Magee

unread,
Sep 16, 2014, 7:31:16 PM9/16/14
to Django Users
Hi Michael,

There's nothing built into Django 1.7, or any previous version; however, it's also not hard to implement yourself. All you have to do is override the save method on your model, and in that method, and store whatever audit information you want to record. So, for example, if you want to just keep a track of the person who most recently edited a record, you could do something like:

class MyModel(models.Model):
    author = models.ForeignKey(settings.AUTH_USER_MODEL)
    ... (other fields here)

    def save(self, user, *args, **kwargs):
        return super(MyModel, self).save(*args, **kwargs)

Then, every time you save an instance of MyModel, you need to pass in the user who saved the record; e.g.:

    MyModel.objects.save(request.user)        

If you want to record a full audit record of every edit, you could do that too; you just create a secondary model type, and every time you save your base model, you create an instance of the secondary type as well.

If you don't want to roll it yourself (or your use case is fairly generic), you might find some pre-rolled django apps that can help: 


I can't speak from experience on any of them, but they exist, and you might find that one of them meets your needs.

Yours,
Russ Magee %-)

On Tue, Sep 16, 2014 at 10:55 AM, Michael Martin <mikema...@gmail.com> wrote:
Hello,

I am sure that I am not the first person to ask this question, but I want to know the best way to track who created or modified a record in my settings defined within models.py.  Is there something new and great added to 1.7 or something that already exists in older versions?


Thank you in advance

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAD0urK3rTvDdkRnTZcsv_b45pV734JApGFAiY3%2BqFO_%3D1vWkzA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Michael Martin

unread,
Sep 16, 2014, 11:50:02 PM9/16/14
to django...@googlegroups.com
Mike,

Thanks for responding.  I put an example of what I am trying to accomplish below with your code in it.  When I add or update a value in the admin, is there a way to have the code add the user automatically?  Seems like the solution you proposed only allows me to manually set the values in the views.py files.  I want to have the ability to accomplish adding records in the views and in the admin.


class GeneralConfiguration(models.Model):

   
    def save(self, user, *args, **kwargs):
        return super(MyModel, self).save(*args, **kwargs)

    configuration_name=models.CharField(max_length=200, blank=True)
    configuration_value=models.CharField(max_length=200, blank=True)
    updated_by=models.ForeignKey(User, null=True, related_name='generalconfiguration_updated_by')
    created_by=models.ForeignKey(User, null=True, related_name='generalconfiguration_created_by')
    created_timestamp=models.DateTimeField(auto_now_add=True, auto_now=False)
    updated_timestamp=models.DateTimeField(auto_now_add=True, auto_now=False)

    def __unicode__(self):
        return "General Configuration"

Fred Stluka

unread,
Sep 17, 2014, 9:08:46 AM9/17/14
to django...@googlegroups.com
Russ,

Thanks for another classic Russ Magee answer!  As usual, you
are succinct, accurate, relevant, prompt, specific, and very
helpful.

There are an amazing number of helpful people on this list, so
the bar is already pretty high, but you continue to raise it.

Keep up the good work!
--Fred
Fred Stluka -- mailto:fr...@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.
Reply all
Reply to author
Forward
0 new messages