from django import forms

from ...product.models import Category


class CategoryForm(forms.ModelForm):
    class Meta:
        model = Category
        exclude = []
from django import forms
from django.utils.translation import ugettext_lazy as _

from ...product.models import Category


class CategoryForm(forms.ModelForm):
    class Meta:
        model = Category
        exclude = []

    def clean_parent(self):
        parent = self.cleaned_data['parent']
        if parent == self.instance:
            raise forms.ValidationError(_('A category may not be made a child of itself'))
        return parent

Add validation on category parent field
Add validation on category parent field

  from django import forms
+ from django.utils.translation import ugettext_lazy as _
  
  from ...product.models import Category
  
  
  class CategoryForm(forms.ModelForm):
      class Meta:
          model = Category
          exclude = []
+ 
+     def clean_parent(self):
+         parent = self.cleaned_data['parent']
+         if parent == self.instance:
+             raise forms.ValidationError(_('A category may not be made a child of itself'))
+         return parent
+ 
Add validation on category parent field
## Code Before:
from django import forms

from ...product.models import Category


class CategoryForm(forms.ModelForm):
    class Meta:
        model = Category
        exclude = []
## Instruction:
Add validation on category parent field
## Code After:
from django import forms
from django.utils.translation import ugettext_lazy as _

from ...product.models import Category


class CategoryForm(forms.ModelForm):
    class Meta:
        model = Category
        exclude = []

    def clean_parent(self):
        parent = self.cleaned_data['parent']
        if parent == self.instance:
            raise forms.ValidationError(_('A category may not be made a child of itself'))
        return parent
