Skip to content

Commit

Permalink
Avoid the "Cannot call get_descendants on unsaved Category instances"
Browse files Browse the repository at this point in the history
ValueError when adding categories in admin interface.
  • Loading branch information
msaelices authored and epicserve committed Mar 6, 2016
1 parent 485ff6e commit 410caf8
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions categories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ def clean(self):
# 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:
Expand Down

0 comments on commit 410caf8

Please sign in to comment.