Amar Amar - 10 months ago 43
Python Question

auditlog with Django and DRF

I need to implement auditlog feature in one of my project which is using

Django 1.8
Django-Rest-Framework 3.2.2
. I have extended
BaseUserManager class
to create user model since I had to use email as a username in my application ( if this information matters ).

Below is my db design which will hold logs :

**fields type desc**

id pk ( auto_increment)
cust_id FK customer
customer_name FK customer
user_id FK user
user_name FK user
module Varchar(100) sales,order,billing,etc
action Varchar(10) Create/Update/Delete
previous_value varchar(500)
current_value varchar(500)
Datetime Datetime timestamp of change

I have tried but it has 2 issues as per my requirement-

  1. It does not capture data as per my requirement which I understand is my issue and so I modified it's code and added my fields into it's model.

  2. It is not capturing module information. Behaviour is random.

I am seeking advice to proceed with this feature. Which package would be best suitable for my task.

P.S I have also tried
and I have no requirement of data versioning.


Answer Source

I achieved what I needed by modifying auditlog code -

  1. Added required field in LogEntry model of auditlog.
  2. Modified log_create,log_update,log_delete functions of to save information in newly added fields.

Using this I am halfway done. Now only issue I am facing is that since model instance of 1 table contains information of other tables as well due to FKs used in the table.

To solve this I could come up with a solution which works well but I am not satisfied with it. I added a function like include_in_model() in each model and modified auditlog's register() function to get those fields and only use that to save information in LogEntry model.

This approach will require me to create this include_in_model() function in each of my model class and pass required fields for particular model. This way I am avoiding FK related information.