Wednesday, September 17, 2014

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

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:fred@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.

On 9/16/14 7:29 PM, Russell Keith-Magee wrote:
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 <mikemartin221@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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAJxq849xiO%2B51BirpKCWv1EiMKDeuTo1-t26MVWnM1vmvmh7zg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment