Tuesday, September 16, 2014

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

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"

On Tue, Sep 16, 2014 at 4:29 PM, Russell Keith-Magee <russell@keith-magee.com> 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.

--
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/CAD0urK2srp%2BLBrzTYx-uNXaJDg0%3D%3D83%3DcOkit-XRXbQH_QGfEA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment