Skip to content

SimpleHistoryAdmin.history_view always use AUTH_USER_MODEL for generating the URL under "Changed By" #1302

Open
@epou

Description

@epou

Describe the bug
The SimpleHistoryAdmin.history_view assumes that the user model associated with the history_user is the one specified in AUTH_USER_MODEL, disregarding any class provided in the HistoricalRecords.user_model argument.

To Reproduce
[Taking the example from the docs: https://django-simple-history.readthedocs.io/en/latest/user_tracking.html#change-user-model]

models.py

from django.db import models
from simple_history.models import HistoricalRecords

class PollUser(models.Model):
    user_id = models.ForeignKey('auth.User')


# Only PollUsers should be modifying a Poll
class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    changed_by = models.ForeignKey(PollUser)
    history = HistoricalRecords(user_model=PollUser)

    @property
    def _history_user(self):
        return self.changed_by

    @_history_user.setter
    def _history_user(self, value):
        self.changed_by = value

admin.py

from simple_history.admin import SimpleHistoryAdmin
from .models import Poll

admin.site.register(Poll, SimpleHistoryAdmin)
  1. Instantiate a new Poll object while specifying the user responsible for the modification.
  2. Navigate to http://<HOSTNAME>/admin/<APP>/poll/<PK>/history/ (retrieve from reverse(f"admin:{obj._meta.app_label}_{obj._meta.model_name}_history", args=[quote(obj.pk)])).
  3. Select the link under the "Changed By" column.
  4. ISSUE: Redirects to the auth.user change URL instead of the appropriate admin URL for the PollUser

Expected behavior

Using the PollUser admin change URL (using the class provided in the HistoricalRecords.user_model args).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions