简体   繁体   中英

Django-mptt admin get child categories

I have next models in my Django project:

class Category(MPTTModel):
    title_of_category = models.CharField(max_length=50, unique=True, verbose_name='Subcategory', default='')
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True,
                            default='',
                            verbose_name='Category')
class Product(models.Model):
    category = models.ForeignKey(Category, verbose_name='Category', default='')
    title = models.CharField(max_length=200, verbose_name='Title of product', default='')

Also admin:

class CategoryAdmin(DjangoMpttAdmin):
    list_display = ('title_of_category',)
    list_filter = ('title_of_category',)

admin.site.register(Category, CategoryAdmin)

@admin.register(Product)
class ProductsAdmin(admin.ModelAdmin):
    list_display = ('title', )

I would like to show in my admin panel only child category(category without children) in ForeignKey. How can I filter category in admin?

I'm not sure if I understand what you are asking but check out this post as it may help. Changing name of Foreign Key items at admin page in Django

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM