Refs #2717. Custom forms for modeladmin.

pull/2752/merge
Thejaswi Puthraya 2016-06-18 22:54:55 +05:30 zatwierdzone przez Matt Westcott
rodzic c41af1fd8c
commit 6821c763ea
9 zmienionych plików z 82 dodań i 6 usunięć

Wyświetl plik

@ -13,6 +13,7 @@ Changelog
* Added `first_common_ancestor` method to `PageQuerySet` (Tim Heap)
* Page chooser now opens at the deepest ancestor page that covers all the pages of the required page type (Tim Heap)
* `PageChooserBlock` now accepts a `target_model` option to specify the required page type (Tim Heap)
* Modeladmin forms now respect `fields` / `exclude` options passed on custom model forms (Thejaswi Puthraya)
* Fix: `AbstractForm` now respects custom `get_template` methods on the page model (Gagaro)
* Fix: Use specific page model for the parent page in the explore index (Gagaro)
* Fix: Remove responsive styles in embed when there is no ratio available (Gagaro)

Wyświetl plik

@ -181,6 +181,7 @@ Contributors
* Luca Perico
* Gary Krige
* Hammy Goonan
* Thejaswi Puthraya
Translators
===========

Wyświetl plik

@ -39,6 +39,7 @@ Minor features
* Added ``first_common_ancestor`` method to ``PageQuerySet`` (Tim Heap)
* Page chooser now opens at the deepest ancestor page that covers all the pages of the required page type (Tim Heap)
* ``PageChooserBlock`` now accepts a ``target_model`` option to specify the required page type (Tim Heap)
* Modeladmin forms now respect ``fields`` / ``exclude`` options passed on custom model forms (Thejaswi Puthraya)
Bug fixes

Wyświetl plik

@ -4,7 +4,7 @@ from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.test import TestCase
from wagtail.tests.modeladmintest.models import Author, Book
from wagtail.tests.modeladmintest.models import Author, Book, Publisher
from wagtail.tests.utils import WagtailTestUtils
@ -116,6 +116,19 @@ class TestCreateView(TestCase, WagtailTestUtils):
# Check that the book was created
self.assertEqual(Book.objects.filter(title="George's Marvellous Medicine").count(), 1)
response = self.client.get('/admin/modeladmintest/publisher/create/')
self.assertIn('name', response.content.decode('UTF-8'))
self.assertNotIn('headquartered_in', response.content.decode('UTF-8'))
self.assertEqual(
[ii for ii in response.context['form'].fields],
['name']
)
self.client.post('/admin/modeladmintest/publisher/create/', {
'name': 'Sharper Collins'
})
publisher = Publisher.objects.get(name='Sharper Collins')
self.assertEqual(publisher.headquartered_in, None)
def test_post_invalid(self):
initial_book_count = Book.objects.count()

Wyświetl plik

@ -0,0 +1,9 @@
from django import forms
from .models import Publisher
class PublisherModelAdminForm(forms.ModelForm):
class Meta:
model = Publisher
fields = ["name"]

Wyświetl plik

@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-06-07 11:22
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('modeladmintest', '0002_token'),
]
operations = [
migrations.CreateModel(
name='Publisher',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=50)),
('headquartered_in', models.CharField(max_length=50, null=True, blank=True)),
],
),
]

Wyświetl plik

@ -30,3 +30,12 @@ class Token(models.Model):
def __str__(self):
return self.key
@python_2_unicode_compatible
class Publisher(models.Model):
name = models.CharField(max_length=50)
headquartered_in = models.CharField(max_length=50, null=True, blank=True)
def __str__(self):
return self.name

Wyświetl plik

@ -1,9 +1,11 @@
from __future__ import absolute_import, unicode_literals
from wagtail.contrib.modeladmin.options import ModelAdmin, ModelAdminGroup, modeladmin_register
from wagtail.contrib.modeladmin.views import CreateView
from wagtail.tests.testapp.models import BusinessChild, EventPage, SingleEventPage
from .models import Author, Book, Token
from .models import Author, Book, Token, Publisher
from .forms import PublisherModelAdminForm
class AuthorModelAdmin(ModelAdmin):
@ -38,6 +40,16 @@ class TokenModelAdmin(ModelAdmin):
list_display = ('key',)
class PublisherCreateView(CreateView):
def get_form_class(self):
return PublisherModelAdminForm
class PublisherModelAdmin(ModelAdmin):
model = Publisher
create_view_class = PublisherCreateView
class EventPageAdmin(ModelAdmin):
model = EventPage
list_display = ('title', 'date_from', 'audience')
@ -66,5 +78,6 @@ class BusinessChildAdmin(ModelAdmin):
modeladmin_register(AuthorModelAdmin)
modeladmin_register(BookModelAdmin)
modeladmin_register(TokenModelAdmin)
modeladmin_register(PublisherModelAdmin)
modeladmin_register(EventsAdminGroup)
modeladmin_register(BusinessChildAdmin)

Wyświetl plik

@ -249,10 +249,16 @@ class BaseCompositeEditHandler(EditHandler):
def __init__(self, instance=None, form=None):
super(BaseCompositeEditHandler, self).__init__(instance=instance, form=form)
self.children = [
handler_class(instance=self.instance, form=self.form)
for handler_class in self.__class__.children
]
self.children = []
for child in self.__class__.children:
if not getattr(child, "children", None) and getattr(child, "field_name", None):
if self.form._meta.exclude:
if child.field_name in self.form._meta.exclude:
continue
if self.form._meta.fields:
if child.field_name not in self.form._meta.fields:
continue
self.children.append(child(instance=self.instance, form=self.form))
def render(self):
return mark_safe(render_to_string(self.template, {