我有一个模型“类别”,其中的外键为“ parent_category”。
如何在Django管理列表视图中订购此模型,例如:
- category 1
-- subcategory 1 of category 1
--- subsubcategory 1 of subcategory 1 of category 1
-- subcategory 2 of category 1
-- subcategory 3 of category 1
- category 2
-- subcategory 1 of category 2
-- subcategory 2 of category 2
我已经尝试了以下方法,但这是行不通的。因此,我需要一些帮助来订购函数“ get_relative_name”。
class PrivateContentCategory(models.Model):
name = models.CharField(
max_length=250,
verbose_name=_('Naam'),
)
slug = models.SlugField(
verbose_name=_('Url'),
blank=True,
)
parent_category = models.ForeignKey(
'self',
on_delete=models.SET_NULL,
related_name='child_category_list',
verbose_name=_('Hoofdcategorie'),
blank=True,
null=True,
)
def __str__(self):
str = self.name
parent_category_obj = self.parent_category
while parent_category_obj is not None:
str = parent_category_obj.name + ' --> ' + str
parent_category_obj = parent_category_obj.parent_category
return str
def get_relative_name(self):
str = self.name
parent_category_obj = self.parent_category
while parent_category_obj is not None:
str = '--' + str
parent_category_obj = parent_category_obj.parent_category
get_relative_name.short_description = _('Naam')
get_relative_name.admin_order_field = [
'parent_category__parent_category',
'name',
]
编辑!!!
父类别的名称不应与类别一起显示。我这样写是为了显示如何订购模型。列表的显示将是:
- OS
-- Windows
--- Windows 7
--- Windows 8
--- Windows 10
-- Mac
-- Linux
--- Debian
---- Ubuntu
--- Fedora
---- CentOS
---- Oracle Linux
最佳答案
对我有用的是在模型中添加一个新字段“ absolute_name”,它将使用pre_save信号自动填充。保存实例后,此字段将在实例本身名称之前包含该实例的所有parent_categories的名称。最后,我只需要在此字段上订购实例:
class PrivateContentCategory(models.Model):
name = models.CharField(
max_length=250,
verbose_name=_('Naam'),
)
slug = models.SlugField(
verbose_name=_('Url'),
blank=True,
)
parent_category = models.ForeignKey(
'self',
on_delete=models.SET_NULL,
related_name='child_category_list',
verbose_name=_('Hoofdcategorie'),
blank=True,
null=True,
)
absolute_name = models.TextField(
verbose_name=_('Absolute naam'),
blank=True,
)
def __str__(self):
return self.absolute_name
def get_relative_name(self):
str = self.name
parent_category_obj = self.parent_category
while parent_category_obj is not None:
str = '--' + str
parent_category_obj = parent_category_obj.parent_category
return str
get_relative_name.short_description = _('Naam')
get_relative_name.admin_order_field = [
'absolute_name',
]
class Meta:
verbose_name = _('Privé inhoud categorie')
verbose_name_plural = _('Privé inhoud categorieën')
ordering = [
'absolute_name',
]
@receiver(models.signals.pre_save, sender=PrivateContentCategory)
def pre_save_private_content_category_obj(sender, instance, **kwargs):
# START Generate instance.absolute_name
instance.absolute_name = instance.name
parent_category_obj = instance.parent_category
while parent_category_obj is not None:
instance.absolute_name = parent_category_obj.name + ' --> ' + instance.absolute_name
parent_category_obj = parent_category_obj.parent_category
# END Generate instance.absolute_name
关于python - 如何在Django中按父类别订购模型?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56170974/