这是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
每当我尝试使用某个父级制作产品时,它都会抛出此错误
A node may not be made a child of any of its descendants.
这里是追溯
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.
我怎样才能解决上述问题。谢谢你提前
答案 0 :(得分:0)
这取决于您使用的django-mptt版本。 我的解决方案是:
class Category(MPTTModel):
# Other Fields
parent = TreeForeignKey(
'self', null=True, blank=True, related_name='children',
verbose_name=pgettext_lazy('Category field', 'parent'))
或
from mptt.managers import TreeManager
class Category(MPTTModel):
# Other Fields
parent = models.ForeignKey(
'self', null=True, blank=True, related_name='children',
verbose_name=pgettext_lazy('Category field', 'parent'))
objects = Manager()
tree = TreeManager()