here is the models.py
class Category(MPTTModel):
title = models.CharField(max_length =120)
name = models.CharField(max_length=120)
parent = models.ForeignKey('self' , null = True , blank = True , verbose_name='parent category', related_name='categories')
description = models.TextField(null=True , blank=True)
active = models.BooleanField(default=True)
slug = models.SlugField(blank=True)
timestamp = models.DateTimeField(auto_now_add=True , auto_now=False)
def get_absolute_url(self):
return reverse('categories', kwargs={'path': self.get_path()})
class Product(MPTTModel):
brand = models.ForeignKey(Brand , related_name='products')
category = models.ForeignKey('Category', verbose_name='categories', related_name='products' , default='')
parent = models.ForeignKey('self' , related_name = 'children' , null=True , blank=True)
title = models.CharField(max_length=500)
gender = models.CharField(max_length=10 , choices=GENDER_CHOICES)
SKU = models.CharField(max_length=255 , blank=True , unique=True)
description = models.TextField(max_length = 500 ,blank=True ,null=True)
price = models.IntegerField()
color = models.CharField(max_length=120)
discount = models.IntegerField(blank=True ,null =True)
active = models.BooleanField(default=True)
is_related = models.BooleanField(default=False)
is_combo = models.BooleanField(default=False)
is_verified = models.BooleanField(default=False)
in_stock = models.BooleanField(default=False)
slug = models.SlugField(max_length=255 , blank=True , unique=True)
timestamp = models.DateTimeField(auto_now_add=True,auto_now=False)
objects = ProductManager()
class Meta:
unique_together = ('SKU', 'slug', 'category', 'brand')
def __unicode__(self):
return self.title
whenever i try to make a product with some parent it throws this error
A node may not be made a child of any of its descendants.
here is the traceback
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in wrapper
616. return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
110. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in inner
233. return view(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in add_view
1516. return self.changeform_view(request, None, form_url, extra_context)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapper
34. return bound_func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
110. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in bound_func
30. return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in inner
145. return func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in changeform_view
1467. self.save_model(request, new_object, form, not add)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in save_model
1078. obj.save()
File "C:\Python27\lib\site-packages\mptt\models.py" in save
973. super(MPTTModel, self).save(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save
710. force_update=force_update, update_fields=update_fields)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save_base
747. update_fields=update_fields, raw=raw, using=using)
File "C:\Python27\lib\site-packages\django\dispatch\dispatcher.py" in send
201. response = receiver(signal=self, sender=sender, **named)
File "C:\Users\lenovo\Desktop\Grooved2\grooved\src\products\models.py" in product_post_save_receiver
403. instance.save()
File "C:\Python27\lib\site-packages\mptt\models.py" in save
918. self, parent, 'last-child', save=False)
File "C:\Python27\lib\site-packages\mptt\managers.py" in _move_node
576. self._move_root_node(node, target, position)
File "C:\Python27\lib\site-packages\mptt\managers.py" in _move_root_node
1190. raise InvalidMove(_('A node may not be made a child of any of its descendants.'))
Exception Type: InvalidMove at /admin/products/product/add/
Exception Value: A node may not be made a child of any of its descendants.
how can i solve the above problem.Thanks in Advance
From this issue comments, running rebuild() method can solve the problem.
from .models import MyModel
MyModel.objects.rebuild()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With