我的显示子类别仅在特定类别中存在一些问题。例如:我有一个类别:商业,工厂,健康......之后我可以在商业类别中添加例如自有公司的子类别。在这种情况下,如果我选择类别业务,我需要只有可见的子类别属于此类别。怎么做? 这是我的代码。 的 models.py
from django.db import models
from slugify import slugify
class Kategoria(models.Model):
name = models.CharField(max_length=50, unique=True, verbose_name='Nazwa kategorii')
slug = models.SlugField(verbose_name='Adres SEO')
class Meta:
verbose_name_plural = 'Kategorie'
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Kategoria, self).save(*args, **kwargs)
def __str__(self):
return self.name
class Subkategoria(models.Model):
category = models.ForeignKey('Kategoria', related_name='subkategoria', on_delete=models.CASCADE, blank=True, null=True, verbose_name='Kategoria główna')
name = models.CharField(max_length=50)
class Meta:
verbose_name_plural = 'Subkategorie'
def __str__(self):
return self.name
class Strona(models.Model):
name = models.CharField(max_length=250, verbose_name='Nazwa strony')
slug = models.SlugField(verbose_name='Adres SEO')
www = models.CharField(max_length=200, verbose_name='Adres strony', unique=True)
content = models.TextField(verbose_name='Opis')
category = models.ForeignKey('Kategoria', verbose_name='Kategoria', on_delete=models.CASCADE)
subcategory = models.ForeignKey('Subkategoria', verbose_name='Subkategoria', on_delete=models.CASCADE)
publish = models.DateField(auto_now=False, auto_now_add=False)
class Meta:
verbose_name_plural = 'Strony'
def __str__(self):
return self.name