From 20496fa6d091d4f09174bcc2cba16d21f5683d78 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 19 Apr 2026 13:28:41 +0000 Subject: [PATCH] Add indexes on created/modified for timestamped models (#891) Every ModelAdmin orders by -created, and blog models use get_latest_by='modified'. Declaring these as Meta.indexes lets makemigrations produce standard AddIndex operations without touching the django-extensions TimeStampedModel inheritance. Also adds a composite (exhibit_type, -created) index on Exhibit to cover filter+order patterns in users profile views. Closes #891 https://claude.ai/code/session_01XC1THLWgnGXGf5wgRhdyvB --- ...blog_clippi_created_34f17d_idx_and_more.py | 50 ++++++++++++ src/blog/models.py | 21 +++++ ...core_artwor_created_f9769d_idx_and_more.py | 81 +++++++++++++++++++ src/core/models.py | 36 +++++++++ 4 files changed, 188 insertions(+) create mode 100644 src/blog/migrations/0012_clipping_blog_clippi_created_34f17d_idx_and_more.py create mode 100644 src/core/migrations/0028_artwork_core_artwor_created_f9769d_idx_and_more.py diff --git a/src/blog/migrations/0012_clipping_blog_clippi_created_34f17d_idx_and_more.py b/src/blog/migrations/0012_clipping_blog_clippi_created_34f17d_idx_and_more.py new file mode 100644 index 00000000..ce21fa1f --- /dev/null +++ b/src/blog/migrations/0012_clipping_blog_clippi_created_34f17d_idx_and_more.py @@ -0,0 +1,50 @@ +# Generated by Django 6.0.4 on 2026-04-19 13:26 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("blog", "0011_update_formatted_bodies"), + ("users", "0012_profileevent_profile_insert_insert_and_more"), + ] + + operations = [ + migrations.AddIndex( + model_name="clipping", + index=models.Index( + fields=["-created"], name="blog_clippi_created_34f17d_idx" + ), + ), + migrations.AddIndex( + model_name="clipping", + index=models.Index( + fields=["-modified"], name="blog_clippi_modifie_4aff76_idx" + ), + ), + migrations.AddIndex( + model_name="post", + index=models.Index( + fields=["-created"], name="blog_post_created_76eb4c_idx" + ), + ), + migrations.AddIndex( + model_name="post", + index=models.Index( + fields=["-modified"], name="blog_post_modifie_763e41_idx" + ), + ), + migrations.AddIndex( + model_name="postimage", + index=models.Index( + fields=["-created"], name="blog_postim_created_77e950_idx" + ), + ), + migrations.AddIndex( + model_name="postimage", + index=models.Index( + fields=["-modified"], name="blog_postim_modifie_ac830e_idx" + ), + ), + ] diff --git a/src/blog/models.py b/src/blog/models.py index 9438c267..f1bd24d5 100644 --- a/src/blog/models.py +++ b/src/blog/models.py @@ -30,6 +30,13 @@ class PostImage(TimeStampedModel): def __str__(self): return self.file.name.lstrip(IMAGE_BASE_PATH) + class Meta: + get_latest_by = "modified" + indexes = [ + models.Index(fields=["-created"]), + models.Index(fields=["-modified"]), + ] + class Clipping(TimeStampedModel): id = models.BigAutoField(primary_key=True) @@ -42,6 +49,13 @@ class Clipping(TimeStampedModel): def __str__(self): return self.title + class Meta: + get_latest_by = "modified" + indexes = [ + models.Index(fields=["-created"]), + models.Index(fields=["-modified"]), + ] + class Post(TimeStampedModel): id = models.BigAutoField(primary_key=True) @@ -92,3 +106,10 @@ def __str__(self): def get_absolute_url(self): return f"/memories/{self.slug}/" + + class Meta: + get_latest_by = "modified" + indexes = [ + models.Index(fields=["-created"]), + models.Index(fields=["-modified"]), + ] diff --git a/src/core/migrations/0028_artwork_core_artwor_created_f9769d_idx_and_more.py b/src/core/migrations/0028_artwork_core_artwor_created_f9769d_idx_and_more.py new file mode 100644 index 00000000..11089b0d --- /dev/null +++ b/src/core/migrations/0028_artwork_core_artwor_created_f9769d_idx_and_more.py @@ -0,0 +1,81 @@ +# Generated by Django 6.0.4 on 2026-04-19 13:26 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("core", "0027_sound_soundevent_remove_artwork_insert_insert_and_more"), + ("users", "0012_profileevent_profile_insert_insert_and_more"), + ] + + operations = [ + migrations.AddIndex( + model_name="artwork", + index=models.Index( + fields=["-created"], name="core_artwor_created_f9769d_idx" + ), + ), + migrations.AddIndex( + model_name="artwork", + index=models.Index( + fields=["-modified"], name="core_artwor_modifie_f2886a_idx" + ), + ), + migrations.AddIndex( + model_name="exhibit", + index=models.Index( + fields=["-created"], name="core_exhibi_created_a7c2a1_idx" + ), + ), + migrations.AddIndex( + model_name="exhibit", + index=models.Index( + fields=["-modified"], name="core_exhibi_modifie_4e6366_idx" + ), + ), + migrations.AddIndex( + model_name="exhibit", + index=models.Index( + fields=["exhibit_type", "-created"], + name="core_exhibi_exhibit_8400bd_idx", + ), + ), + migrations.AddIndex( + model_name="marker", + index=models.Index( + fields=["-created"], name="core_marker_created_5613b5_idx" + ), + ), + migrations.AddIndex( + model_name="marker", + index=models.Index( + fields=["-modified"], name="core_marker_modifie_99c7c6_idx" + ), + ), + migrations.AddIndex( + model_name="object", + index=models.Index( + fields=["-created"], name="core_object_created_69c39d_idx" + ), + ), + migrations.AddIndex( + model_name="object", + index=models.Index( + fields=["-modified"], name="core_object_modifie_f5a679_idx" + ), + ), + migrations.AddIndex( + model_name="sound", + index=models.Index( + fields=["-created"], name="core_sound_created_ab0a8d_idx" + ), + ), + migrations.AddIndex( + model_name="sound", + index=models.Index( + fields=["-modified"], name="core_sound_modifie_bd78b7_idx" + ), + ), + ] diff --git a/src/core/models.py b/src/core/models.py index 7406e2ae..937c4808 100644 --- a/src/core/models.py +++ b/src/core/models.py @@ -201,6 +201,13 @@ def as_html_thumbnail(self, editable=False): return render(div(elements, style="margin: 10px auto;")) + class Meta: + get_latest_by = "modified" + indexes = [ + models.Index(fields=["-created"]), + models.Index(fields=["-modified"]), + ] + class ExhibitTypes(models.TextChoices): AR = "AR", "Augmented Reality" @@ -284,6 +291,13 @@ def as_html_thumbnail(self, editable: bool = False): to_render.append(lower_menu) return render(to_render) + class Meta: + get_latest_by = "modified" + indexes = [ + models.Index(fields=["-created"]), + models.Index(fields=["-modified"]), + ] + class ObjectExtensions(models.TextChoices): GIF = "gif", "GIF" @@ -408,6 +422,13 @@ def as_html_thumbnail(self, editable=False): return render(to_render) + class Meta: + get_latest_by = "modified" + indexes = [ + models.Index(fields=["-created"]), + models.Index(fields=["-modified"]), + ] + @pghistory.track() class Artwork(TimeStampedModel, ContentMixin): @@ -491,6 +512,13 @@ def as_html_thumbnail(self, editable=False): ) return render(div(elements, class_="artwork-elements flex")) + class Meta: + get_latest_by = "modified" + indexes = [ + models.Index(fields=["-created"]), + models.Index(fields=["-modified"]), + ] + @pghistory.track() class Exhibit(TimeStampedModel, ContentMixin, models.Model): @@ -599,6 +627,14 @@ def as_html_thumbnail(self, editable=False): ] return render(elements) + class Meta: + get_latest_by = "modified" + indexes = [ + models.Index(fields=["-created"]), + models.Index(fields=["-modified"]), + models.Index(fields=["exhibit_type", "-created"]), + ] + @receiver(post_delete, sender=Object) @receiver(post_delete, sender=Marker)