vvangelovski / django-audit-log

Audit log for your Django models
Other
232 stars 94 forks source link

Auditing User model changes #20

Open agale031176 opened 9 years ago

agale031176 commented 9 years ago

Hi

I am using this module in a project and I have a class inheriting from AbstractBaseUser and then in the settings I point AUTH_USER_MODEL to this new class. However if I try to put AuditLog on this table I get an issue whenever there is an update on this table. I have debugged the code for AuditLog and found the problem in the following lines:

        #check if the manager has been attached to auth user model
        if [model._meta.app_label, model.__name__] == getattr(settings, 'AUTH_USER_MODEL', 'auth.User').split("."):
            action_user_field = LastUserField(related_name = rel_name, editable = False, to = 'self')

Removing this then it start complaining that my class is not yet installed or is abstract. Anyone had this issue? Is this a problem with AuditLog or there is another way how to do it?

ghinch commented 9 years ago

The issue with removing it seems to be that this method is being called via a receiver on django.db.models.signals.class_prepared. The app registry is not ready for the app which contains the User model at this time, so using settings.AUTH_USER_MODEL won't work, as that calls django.contrib.auth.get_user_model, which in turn calls django.apps.registry.get_model, and the latter is depending on the registry to have a knowledge of the app in order to lookup the model.

However, the solution to that currently, which had been implemented (and you remove), is not a good one either, because to = 'self' in this case is actually referencing the model the field is on by using self, which is is the AuditLogEntry model, and not your User model.

Will update here if I find a solution, but it's certainly a tricky one!