Filter parent categories on apphook
This commit is contained in:
parent
e56d5aa137
commit
c14afead36
2 changed files with 15 additions and 5 deletions
|
@ -33,7 +33,7 @@ except ImportError:
|
||||||
class BlogCategoryAdmin(EnhancedModelAdminMixin, ModelAppHookConfig, TranslatableAdmin):
|
class BlogCategoryAdmin(EnhancedModelAdminMixin, ModelAppHookConfig, TranslatableAdmin):
|
||||||
form = CategoryAdminForm
|
form = CategoryAdminForm
|
||||||
list_display = [
|
list_display = [
|
||||||
'name', 'parent', 'all_languages_column',
|
'name', 'parent', 'app_config', 'all_languages_column',
|
||||||
]
|
]
|
||||||
|
|
||||||
def get_prepopulated_fields(self, request, obj=None):
|
def get_prepopulated_fields(self, request, obj=None):
|
||||||
|
|
|
@ -13,11 +13,21 @@ class CategoryAdminForm(TranslatableModelForm):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(CategoryAdminForm, self).__init__(*args, **kwargs)
|
super(CategoryAdminForm, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
if 'parent' in self.fields:
|
||||||
|
qs = self.fields['parent'].queryset
|
||||||
if self.instance.pk:
|
if self.instance.pk:
|
||||||
self.fields['parent'].queryset = self.fields['parent'].queryset.exclude(
|
qs = qs.exclude(
|
||||||
pk__in=[self.instance.pk] + [child.pk for child in self.instance.descendants()]
|
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:
|
class Meta:
|
||||||
model = BlogCategory
|
model = BlogCategory
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
Loading…
Add table
Reference in a new issue