'No indentations for the options in the select box in an inlined table (Django MPTT)
I created many-to-many relationship with the tables "Category", "Product" and "CategoryProduct" which is the middle table between "Category" and "Product":
"models.py":
from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
class Category(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:
order_insertion_by = ["name"]
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class CategoryProduct(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
class Meta:
unique_together = [['category', 'product']]
Then, I registered "Category" and "Product" with the inline "CategoryProduct":
"admin.py":
from django.contrib import admin
from mptt.admin import MPTTModelAdmin
from .models import Category, Product, CategoryProduct
admin.site.register(Category, MPTTModelAdmin)
class CategoryProductInline(admin.TabularInline):
model = CategoryProduct
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
inlines = [CategoryProductInline]
Then, I added some categories and as you can see, the options in the select box of "Parent" have indentations:
But when I tried to add a product, as you can see, the options in the select box of "Category" don't have indentations:
Is it possible to give indentations to the options in the select box of "Category"? If possible, how can I do this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


