kopia lustrzana https://github.com/wagtail/wagtail
Add missing bind_to(instance) to ModelAdmin edit view
Fixes #6413. Following the refactor in 80cc70b7ce
this was not being called - this is necessary to propagate the instance down nested edit handlers.
pull/6431/head
rodzic
8251b4a6c7
commit
21f66a0d64
|
@ -8,9 +8,10 @@ from openpyxl import load_workbook
|
|||
|
||||
from wagtail.admin.edit_handlers import FieldPanel, TabbedInterface
|
||||
from wagtail.contrib.modeladmin.helpers.search import DjangoORMSearchHandler
|
||||
from wagtail.core.models import Page
|
||||
from wagtail.images.models import Image
|
||||
from wagtail.images.tests.utils import get_test_image_file
|
||||
from wagtail.tests.modeladmintest.models import Author, Book, Publisher, Token
|
||||
from wagtail.tests.modeladmintest.models import Author, Book, Publisher, RelatedLink, Token
|
||||
from wagtail.tests.modeladmintest.wagtail_hooks import BookModelAdmin
|
||||
from wagtail.tests.utils import WagtailTestUtils
|
||||
|
||||
|
@ -272,6 +273,21 @@ class TestCreateView(TestCase, WagtailTestUtils):
|
|||
|
||||
mock_form_clean.assert_called_once()
|
||||
|
||||
def test_create_view_with_multifieldpanel(self):
|
||||
# https://github.com/wagtail/wagtail/issues/6413
|
||||
response = self.client.get('/admin/modeladmintest/relatedlink/create/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
response = self.client.post('/admin/modeladmintest/relatedlink/create/', {
|
||||
'title': "Homepage",
|
||||
'link': Page.objects.filter(depth=2).first().id,
|
||||
})
|
||||
# Should redirect back to index
|
||||
self.assertRedirects(response, '/admin/modeladmintest/relatedlink/')
|
||||
|
||||
# Check that the link was created
|
||||
self.assertEqual(RelatedLink.objects.filter(title="Homepage").count(), 1)
|
||||
|
||||
|
||||
class TestInspectView(TestCase, WagtailTestUtils):
|
||||
fixtures = ['modeladmintest_test.json']
|
||||
|
@ -409,6 +425,22 @@ class TestEditView(TestCase, WagtailTestUtils):
|
|||
|
||||
mock_form_clean.assert_called_once()
|
||||
|
||||
def test_edit_view_with_multifieldpanel(self):
|
||||
# https://github.com/wagtail/wagtail/issues/6413
|
||||
link = RelatedLink.objects.create(title='Homepage', link=Page.objects.filter(depth=2).first())
|
||||
response = self.client.get('/admin/modeladmintest/relatedlink/edit/%d/' % link.id)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
response = self.client.post('/admin/modeladmintest/relatedlink/edit/%d/' % link.id, {
|
||||
'title': "Homepage edited",
|
||||
'link': Page.objects.filter(depth=2).first().id,
|
||||
})
|
||||
# Should redirect back to index
|
||||
self.assertRedirects(response, '/admin/modeladmintest/relatedlink/')
|
||||
|
||||
# Check that the link was updated
|
||||
self.assertEqual(RelatedLink.objects.filter(title="Homepage edited").count(), 1)
|
||||
|
||||
|
||||
class TestPageSpecificViews(TestCase, WagtailTestUtils):
|
||||
fixtures = ['modeladmintest_test.json']
|
||||
|
|
|
@ -118,10 +118,13 @@ class ModelFormView(WMABaseView, FormView):
|
|||
return form
|
||||
|
||||
def get_edit_handler(self):
|
||||
instance = self.get_instance()
|
||||
edit_handler = self.model_admin.get_edit_handler(
|
||||
instance=self.get_instance(), request=self.request
|
||||
instance=instance, request=self.request
|
||||
)
|
||||
return edit_handler.bind_to(
|
||||
model=self.model_admin.model, request=self.request, instance=instance
|
||||
)
|
||||
return edit_handler.bind_to(model=self.model_admin.model, request=self.request)
|
||||
|
||||
def get_form_class(self):
|
||||
return self.edit_handler.get_form_class()
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 3.1.1 on 2020-10-01 18:16
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('wagtailcore', '0057_page_locale_fields_notnull'),
|
||||
('modeladmintest', '0008_solobook'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='RelatedLink',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=255)),
|
||||
('link', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='wagtailcore.page')),
|
||||
],
|
||||
),
|
||||
]
|
|
@ -1,6 +1,8 @@
|
|||
from django.db import models
|
||||
|
||||
from wagtail.admin.edit_handlers import FieldPanel, ObjectList, TabbedInterface
|
||||
from wagtail.admin.edit_handlers import (
|
||||
FieldPanel, MultiFieldPanel, ObjectList, PageChooserPanel, TabbedInterface
|
||||
)
|
||||
from wagtail.core.models import Page
|
||||
from wagtail.search import index
|
||||
|
||||
|
@ -124,3 +126,24 @@ class Friend(models.Model):
|
|||
|
||||
def __str__(self):
|
||||
return self.first_name
|
||||
|
||||
|
||||
class RelatedLink(models.Model):
|
||||
title = models.CharField(
|
||||
max_length=255,
|
||||
)
|
||||
link = models.ForeignKey(
|
||||
Page,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='+'
|
||||
)
|
||||
|
||||
panels = [
|
||||
MultiFieldPanel(
|
||||
[
|
||||
FieldPanel('title'),
|
||||
PageChooserPanel('link'),
|
||||
],
|
||||
heading='Related Link'
|
||||
),
|
||||
]
|
||||
|
|
|
@ -6,7 +6,9 @@ from wagtail.contrib.modeladmin.views import CreateView, EditView, IndexView
|
|||
from wagtail.tests.testapp.models import BusinessChild, EventPage, SingleEventPage
|
||||
|
||||
from .forms import PublisherModelAdminForm
|
||||
from .models import Author, Book, Contributor, Friend, Person, Publisher, Token, VenuePage, Visitor
|
||||
from .models import (
|
||||
Author, Book, Contributor, Friend, Person, Publisher, RelatedLink, Token, VenuePage, Visitor
|
||||
)
|
||||
|
||||
|
||||
class AuthorModelAdmin(ModelAdmin):
|
||||
|
@ -164,6 +166,11 @@ class BusinessChildAdmin(ModelAdmin):
|
|||
menu_label = "BusinessSprog"
|
||||
|
||||
|
||||
class RelatedLinkAdmin(ModelAdmin):
|
||||
model = RelatedLink
|
||||
menu_label = "Related Links"
|
||||
|
||||
|
||||
modeladmin_register(AuthorModelAdmin)
|
||||
modeladmin_register(BookModelAdmin)
|
||||
modeladmin_register(TokenModelAdmin)
|
||||
|
@ -174,3 +181,4 @@ modeladmin_register(PersonAdmin)
|
|||
modeladmin_register(FriendAdmin)
|
||||
modeladmin_register(VisitorAdmin)
|
||||
modeladmin_register(ContributorAdmin)
|
||||
modeladmin_register(RelatedLinkAdmin)
|
||||
|
|
Ładowanie…
Reference in New Issue