Avoid the "Cannot call get_descendants on unsaved Category instances"

ValueError when adding categories in admin interface.
This commit is contained in:
msaelices 2015-08-26 21:29:04 +02:00 committed by Brent O'Connor
parent 485ff6e6dd
commit 410caf8e79

View file

@ -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: