Filter parent categories on apphook

This commit is contained in:
Iacopo Spalletti 2016-08-03 09:18:55 +02:00
parent e56d5aa137
commit c14afead36
No known key found for this signature in database
GPG key ID: BDCBC2EB289F60C6
2 changed files with 15 additions and 5 deletions

View file

@ -33,7 +33,7 @@ except ImportError:
class BlogCategoryAdmin(EnhancedModelAdminMixin, ModelAppHookConfig, TranslatableAdmin):
form = CategoryAdminForm
list_display = [
'name', 'parent', 'all_languages_column',
'name', 'parent', 'app_config', 'all_languages_column',
]
def get_prepopulated_fields(self, request, obj=None):

View file

@ -13,10 +13,20 @@ class CategoryAdminForm(TranslatableModelForm):
def __init__(self, *args, **kwargs):
super(CategoryAdminForm, self).__init__(*args, **kwargs)
if self.instance.pk:
self.fields['parent'].queryset = self.fields['parent'].queryset.exclude(
pk__in=[self.instance.pk] + [child.pk for child in self.instance.descendants()]
)
if 'parent' in self.fields:
qs = self.fields['parent'].queryset
if self.instance.pk:
qs = qs.exclude(
pk__in=[self.instance.pk] + [child.pk for child in self.instance.descendants()]
)
if getattr(self.instance, 'app_config_id', None):
qs = qs.namespace(self.instance.app_config.namespace)
elif 'initial' in kwargs and 'app_config' in kwargs['initial']:
config = BlogConfig.objects.get(pk=kwargs['initial']['app_config'])
qs = qs.namespace(config.namespace)
self.fields['parent'].queryset = qs
class Meta:
model = BlogCategory