From 410caf8e79b8e03be959fd3aae86a3f30b9801f9 Mon Sep 17 00:00:00 2001 From: msaelices Date: Wed, 26 Aug 2015 21:29:04 +0200 Subject: [PATCH] Avoid the "Cannot call get_descendants on unsaved Category instances" ValueError when adding categories in admin interface. --- categories/base.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/categories/base.py b/categories/base.py index 7305cf8..af132bb 100644 --- a/categories/base.py +++ b/categories/base.py @@ -108,10 +108,11 @@ class CategoryBaseAdminForm(forms.ModelForm): # Validate Category Parent # Make sure the category doesn't set itself or any of its children as # its parent. - decendant_ids = self.instance.get_descendants().values_list('id', flat=True) + if self.cleaned_data.get('parent', None) is None or self.instance.id is None: return self.cleaned_data - elif self.cleaned_data['parent'].id == self.instance.id: + decendant_ids = self.instance.get_descendants().values_list('id', flat=True) + if self.cleaned_data['parent'].id == self.instance.id: raise forms.ValidationError(_("You can't set the parent of the " "item to itself.")) elif self.cleaned_data['parent'].id in decendant_ids: