Ensure reference index uses pk instead of id for references in inline objects (#12983)

Fixes #12426

Fixes an issue with models that use a custom primary key with a different name than 'id'
pull/12993/head
Sage Abdullah 2025-03-21 15:45:39 +07:00 zatwierdzone przez Matt Westcott
rodzic 32f4a78c96
commit a3352a8676
6 zmienionych plików z 60 dodań i 1 usunięć

Wyświetl plik

@ -29,6 +29,7 @@ Changelog
* Fix: Ensure `ImproperlyConfigured` is thrown from `db_field` on unbound `FieldPanel`s as intended (Matt Westcott)
* Fix: Refine the positioning of the add comment button next to select, radio, checkbox fields and between field row columns (Srishti Jaiswal)
* Fix: Show the correct privacy status for child collections of private collections (Shlomo Markowitz)
* Fix: Ensure reference index correctly handles models with primary keys not named `id` (Sage Abdullah)
* Docs: Add missing `django.contrib.admin` to list of apps in "add to Django project" guide (Mohamed Rabiaa)
* Docs: Add tutorial on deploying on Ubuntu to third-party tutorials (Mohammad Fathi Rahman)
* Docs: Document that request_or_site is optional on BaseGenericSetting.load (Matt Westcott)

Wyświetl plik

@ -44,6 +44,7 @@ This version adds formal support for Django 5.2.
* Ensure `ImproperlyConfigured` is thrown from `db_field` on unbound `FieldPanel`s as intended (Matt Westcott)
* Refine the positioning of the add comment button next to select, radio, checkbox fields and between field row columns (Srishti Jaiswal)
* Show the correct privacy status for child collections of private collections (Shlomo Markowitz)
* Ensure reference index correctly handles models with primary keys not named `id` (Sage Abdullah)
### Documentation

Wyświetl plik

@ -384,7 +384,7 @@ class ReferenceIndex(models.Model):
to_content_type_id,
to_object_id,
f"{relation_name}.item.{model_path}",
f"{relation_name}.{str(child_object.id)}.{content_path}",
f"{relation_name}.{str(child_object.pk)}.{content_path}",
)
for to_content_type_id, to_object_id, model_path, content_path in cls._extract_references_from_object(
child_object

Wyświetl plik

@ -0,0 +1,25 @@
# Generated by Django 6.0.dev20250303103700 on 2025-03-21 07:17
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("tests", "0049_promotionalpage"),
("wagtailcore", "0094_alter_page_locale"),
]
operations = [
migrations.AddField(
model_name="headcountrelatedmodelusingpk",
name="related_page",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="head_count_relations",
to="wagtailcore.page",
),
),
]

Wyświetl plik

@ -459,6 +459,12 @@ class HeadCountRelatedModelUsingPK(models.Model):
event_page = ParentalKey(
EventPage, on_delete=models.CASCADE, related_name="head_counts"
)
related_page = models.ForeignKey(
Page,
on_delete=models.CASCADE,
related_name="head_count_relations",
null=True,
)
head_count = models.IntegerField()
panels = [FieldPanel("head_count")]

Wyświetl plik

@ -21,6 +21,7 @@ from wagtail.test.testapp.models import (
GenericSnippetNoFieldIndexPage,
GenericSnippetNoIndexPage,
GenericSnippetPage,
HeadCountRelatedModelUsingPK,
ModelWithNullableParentalKey,
VariousOnDeleteModel,
)
@ -281,6 +282,31 @@ class TestCreateOrUpdateForObject(TestCase):
self.assertIn(" 3 wagtail.images.models.Image", stdout.getvalue())
self.assertIn(" 4 wagtail.test.testapp.models.EventPage", stdout.getvalue())
def test_inline_custom_pk_model(self):
related_page = EventPage(
title="Related page",
slug="related-page",
location="the moon",
audience="public",
cost="free",
date_from="2025-03-21",
)
with self.captureOnCommitCallbacks(execute=True):
self.root_page.add_child(instance=related_page)
refs = ReferenceIndex.get_references_to(related_page)
self.assertEqual(refs.count(), 0)
with self.captureOnCommitCallbacks(execute=True):
HeadCountRelatedModelUsingPK.objects.create(
head_count=1234,
event_page=self.event_page,
related_page=related_page,
)
refs = ReferenceIndex.get_references_to(related_page)
self.assertEqual(refs.count(), 1)
class TestDescribeOnDelete(TestCase):
fixtures = ["test.json"]