您好,我有类别和产品。我使用django-mptt库仅显示产品的后代类别。我已经编写了自定义子类别字段以仅显示子孙类别。 更新类别后,子类别不会在产品对应的字段中更新。 我应该重新启动服务器。
如何在不重新启动服务器的情况下完成此操作
from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
class Genre(MPTTModel):
name = models.CharField(max_length=50, unique=True)
parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
class MPTTMeta:
level_attr = 'mptt_level'
order_insertion_by = ['name']
def __str__(self):
return self.name
class Product(models.Model):
nodes = Genre.objects.all()
CHOICE_SUBCATEGORIES = [
]
for node in nodes:
if node.is_leaf_node():
node.get_descendants(include_self=False)
print(node.get_descendants(include_self=False))
CHOICE_SUBCATEGORIES.append((node.id, node.name))
name = models.CharField(max_length=128)
genre = models.ForeignKey(Genre, on_delete=models.CASCADE, null=True, blank=True, default=None)
subcategories = models.CharField(max_length=128, choices=CHOICE_SUBCATEGORIES, default=None, null=True, blank=True)
def __str__(self):
return self.name