From 938431389f739876626ef080543563cc8f5f6ace Mon Sep 17 00:00:00 2001 From: Andreas Hasenkopf Date: Tue, 4 Jan 2022 10:06:45 +0100 Subject: [PATCH] Add a DB index to `LogEntry`'s `action` field (#236) For some applications the possibility to filter by the `action` field might be really interesting. However the lack of an index can lead to severe reduction of such queries. The simplest solution: Let's a DB index on that field :) --- auditlog/migrations/0008_action_index.py | 20 ++++++++++++++++++++ auditlog/models.py | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 auditlog/migrations/0008_action_index.py diff --git a/auditlog/migrations/0008_action_index.py b/auditlog/migrations/0008_action_index.py new file mode 100644 index 0000000..52785e9 --- /dev/null +++ b/auditlog/migrations/0008_action_index.py @@ -0,0 +1,20 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("auditlog", "0007_object_pk_type"), + ] + + operations = [ + migrations.AlterField( + model_name="logentry", + name="action", + field=models.PositiveSmallIntegerField( + choices=[(0, "create"), (1, "update"), (2, "delete")], + db_index=True, + verbose_name="action", + ), + ), + ] diff --git a/auditlog/models.py b/auditlog/models.py index 8c4a73c..2622674 100644 --- a/auditlog/models.py +++ b/auditlog/models.py @@ -213,7 +213,7 @@ class LogEntry(models.Model): ) object_repr = models.TextField(verbose_name=_("object representation")) action = models.PositiveSmallIntegerField( - choices=Action.choices, verbose_name=_("action") + choices=Action.choices, verbose_name=_("action"), db_index=True ) changes = models.TextField(blank=True, verbose_name=_("change message")) actor = models.ForeignKey(