Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exclude_fields_in_copy with DeferringRelatedManager object #11809

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion wagtail/actions/copy_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ def _copy_page(
)

for exclude_field in specific_page.exclude_fields_in_copy:
if exclude_field in revision_content and hasattr(
if hasattr(page_copy, exclude_field) and hasattr(
getattr(page_copy, exclude_field), "all"
):
revision_content[exclude_field] = []
elif exclude_field in revision_content and hasattr(
page_copy, exclude_field
):
revision_content[exclude_field] = getattr(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ class Migration(migrations.Migration):
to="tests.gallerypage",
),
),
(
"exclude_field",
modelcluster.fields.ParentalKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="related_manager",
to="tests.pagewithexcludedcopyfield",
),
),
],
options={
"ordering": ["sort_order"],
Expand Down
9 changes: 8 additions & 1 deletion wagtail/test/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,13 @@ class PageWithExcludedCopyField(Page):

# Exclude this field from being copied
special_field = models.CharField(blank=True, max_length=255, default="Very Special")
exclude_fields_in_copy = ["special_field"]
exclude_fields_in_copy = ["special_field", "related_manager"]

content_panels = [
FieldPanel("title", classname="title"),
FieldPanel("special_field"),
FieldPanel("content"),
MultipleChooserPanel("related_manager", chooser_field_name="image"),
]


Expand Down Expand Up @@ -2193,6 +2194,12 @@ class GalleryPageImage(Orderable):
page = ParentalKey(
"tests.GalleryPage", related_name="gallery_images", on_delete=models.CASCADE
)
exclude_field = ParentalKey(
"tests.PageWithExcludedCopyField",
related_name="related_manager",
on_delete=models.CASCADE,
null=True,
)
image = models.ForeignKey(
"wagtailimages.Image",
on_delete=models.CASCADE,
Expand Down
4 changes: 4 additions & 0 deletions wagtail/tests/test_page_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1912,13 +1912,17 @@ def test_copy_page_with_additional_excluded_fields(self):
page.save_revision()
new_page = page.copy(to=homepage, update_attrs={"slug": "disco-2"})
exclude_field = new_page.latest_revision.content["special_field"]
exclude_field_related_manager = new_page.latest_revision.content[
"related_manager"
]

self.assertEqual(page.title, new_page.title)
self.assertNotEqual(page.id, new_page.id)
self.assertNotEqual(page.path, new_page.path)
# special_field is in the list to be excluded
self.assertNotEqual(page.special_field, new_page.special_field)
self.assertEqual(new_page.special_field, exclude_field)
self.assertEqual(exclude_field_related_manager, [])

def test_page_with_generic_relation(self):
"""Test that a page with a GenericRelation will have that relation ignored when
Expand Down