Custom Admin Page with Fields as Objects

I have 3 models: Category, SubCategory, Product. Category and SubCategory have one-to-many relationship. SubCategory and Product have many-to-many relationship(ManyToManyField is in Product). Currently, admin page for Product shows one field for SubCategory as multiple choice field. But I want fields for Product to be as Category objects. SubCategories should be divided by their Categories and user should be able to choose a SubCategory from each of this field. Can't work it out. Thanks in advance!

models.py:

class Category(models.Model):
    name = models.CharField(max_length=150, unique=True)
    
    created_at = models.DateTimeField(default=timezone.now)
    modified_at = models.DateTimeField(default=timezone.now)
    
    def __str__(self):
        return self.name

class SubCategory(models.Model):
    name = models.CharField(max_length=150, blank=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='subcategories')
    created_at = models.DateTimeField(default=timezone.now)
    modified_at = models.DateTimeField(default=timezone.now)
    
    def __str__(self):
        return self.name

class Product(models.Model):
    name = models.CharField(max_length=150, blank=True)
    description = models.TextField(blank=True)
    price = models.IntegerField(default=-1)
    quantity = models.IntegerField(default=-1)
    image = models.ImageField(blank=True)
    created_at = models.DateTimeField(default=timezone.now)
    modified_at = models.DateTimeField(default=timezone.now)
    discount = models.ForeignKey(Discount, on_delete=models.PROTECT, blank=True)    
    subcategory = models.ManyToManyField(SubCategory)

    class Meta:
        ordering = ('-created_at',)

    def __str__(self):
        return self.name
Back to Top