Skip to content

Commit

Permalink
Merge branch 'feature/improve-admin-form' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Fantomas42 committed Apr 30, 2015
2 parents b1342be + e1806f1 commit 95ca8bc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
18 changes: 8 additions & 10 deletions zinnia/admin/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Forms for Zinnia admin"""
from django import forms
from django.db.models import ManyToOneRel
from django.db.models import ManyToManyRel
from django.utils.translation import ugettext_lazy as _
from django.contrib.admin.widgets import RelatedFieldWidgetWrapper

Expand All @@ -21,16 +19,16 @@ class CategoryAdminForm(forms.ModelForm):
"""
parent = TreeNodeChoiceField(
label=_('Parent category'),
level_indicator='|--', required=False,
empty_label=_('No parent category'),
level_indicator='|--', required=False,
queryset=Category.objects.all())

def __init__(self, *args, **kwargs):
super(CategoryAdminForm, self).__init__(*args, **kwargs)
rel = ManyToOneRel(Category._meta.get_field('tree_id'),
Category, 'id')
self.fields['parent'].widget = RelatedFieldWidgetWrapper(
self.fields['parent'].widget, rel, self.admin_site)
self.fields['parent'].widget,
Category.parent.field.rel,
self.admin_site)

def clean_parent(self):
"""
Expand Down Expand Up @@ -58,14 +56,14 @@ class EntryAdminForm(forms.ModelForm):
categories = MPTTModelMultipleChoiceField(
label=_('Categories'), required=False,
queryset=Category.objects.all(),
widget=MPTTFilteredSelectMultiple(_('categories'), False,
attrs={'rows': '10'}))
widget=MPTTFilteredSelectMultiple(_('categories')))

def __init__(self, *args, **kwargs):
super(EntryAdminForm, self).__init__(*args, **kwargs)
rel = ManyToManyRel(Category, 'id')
self.fields['categories'].widget = RelatedFieldWidgetWrapper(
self.fields['categories'].widget, rel, self.admin_site)
self.fields['categories'].widget,
Entry.categories.field.rel,
self.admin_site)

class Meta:
"""
Expand Down
7 changes: 7 additions & 0 deletions zinnia/admin/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class MPTTFilteredSelectMultiple(widgets.FilteredSelectMultiple):
MPTT version of FilteredSelectMultiple.
"""

def __init__(self, verbose_name, is_stacked=False, attrs=None, choices=()):
"""
Initializes the widget directly not stacked.
"""
super(MPTTFilteredSelectMultiple, self).__init__(
verbose_name, is_stacked, attrs, choices)

def render_option(self, selected_choices, option_value,
option_label, sort_fields):
"""
Expand Down

0 comments on commit 95ca8bc

Please sign in to comment.