python-3.xdjangodjango-simple-history

How to parse _history_user_id from django-simple-history into an html table?


I'm currently using djanog-simple-history to track which user made the most recent change to a model. This is working as expected as it shows me on the UI who the most recent user is. I'm acheving this by using the middleware simple_history.middleware.HistoryRequestMiddleware. I have created a report (html table) that will grab certain datapoints from different models and be used as a hub to track all the changes made by certain users. Trying to track the history user is the last key to all of this.

Model:

class Degree(TimeStampedModel):

    user = ForeignKey(User, null=True, blank=True, related_name='degrees', on_delete=CASCADE)
    level = CharField(max_length=50, null=True, blank=True, choices=LEVELS)
    institution = CharField(max_length=255, null=True)
    major = CharField(max_length=255, null=True, blank=True)
    minor = CharField(max_length=255, null=True, blank=True)
    verified_date = DateField(null=True, blank=True)
    graduation_date = DateField(null=True, blank=True)
    reason_for_change = CharField(max_length=255, null=True)
    history = HistoricalRecords(User)

Report:

def Changelog_table(request):
    name = 'Changelog'
    desc = 'changelog'
    headers = [
        ('', ''),
        ('user__last_name', 'Last name'),
        ('user__first_name', 'First name'),
        ('reason_for_change', 'Reason for change'),
        ('section', 'section'),
        ('modified', 'modified'),
        ('user', 'user')
    ]
    filters = {
        'user__is_active': True,
    }
    values = [
        'user__username',
        'user__last_name',
        'user__first_name',
        'reason_for_change',
        'section',
        'modified',
        'user',
        ]
    qs_degree = (
        Degree.objects.filter(**filters) \
            .values_list(*values)
    )

    return render(request, 'reports/report.html', {
        'headers': headers,
        'data': json.dumps(list(qs_degree)), cls=DjangoJSONEncoder),
        'name': name,
        'desc': desc,
    })

I have read up on the docs and the different ways of tracking a user. As stated above, I'm currently using the middleware for this. But using the middleware doesn't give me anyway of tracking the history user in my report that I know of. The report above that is looking at user is tracking the users ID, not the history users ID. I've dropped the middleware and added a _history_user property referencing the user field (custom user class).

class Degree(TimeStampedModel):

    user = ForeignKey(User, null=True, blank=True, related_name='degrees', on_delete=CASCADE)
    level = CharField(max_length=50, null=True, blank=True, choices=LEVELS)
    institution = CharField(max_length=255, null=True)
    major = CharField(max_length=255, null=True, blank=True)
    minor = CharField(max_length=255, null=True, blank=True)
    verified_date = DateField(null=True, blank=True)
    graduation_date = DateField(null=True, blank=True)
    reason_for_change = CharField(max_length=255, null=True)
    history = HistoricalRecords(User)

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

This didn't work as it's just tracking the users profile ID instead now. I noticed in the historical tables that are created, there's a column named history_user_id. How would I go about accessing and tracking the history_user_id from the historical tables instead of the users_id?


Solution

  • I figured out my question by looking at the history table of a primary table:

    qs_history_user_degree = (
            Degree.history.filter(**filters) \
            .values_list(*values)
        )