[Python] custom filter for filter_horizontal admin in django

Discussão em 'Python' iniciado por Stack, Setembro 6, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following models where a deck have a many to many relationship with problems and problems can have tags

    from django.utils import timezone
    from django.db import models
    from taggit.models import TaggedItemBase
    from taggit.managers import TaggableManager

    # Create your models here.

    class TaggedProblem(TaggedItemBase):
    content_object = models.ForeignKey('Problem', on_delete=models.CASCADE)

    class Problem(models.Model):
    title = models.CharField(max_length=200)
    body = models.CharField(max_length=10000)
    pub_date = models.DateTimeField("date published", default=timezone.now())
    tags = TaggableManager(through=TaggedProblem)

    class Meta:
    verbose_name = "problem"
    verbose_name_plural = "problems"

    def __str__(self):
    return self.title

    class Deck(models.Model):
    name = models.CharField(max_length=200)
    problems = models.ManyToManyField(Problem)
    def __str__(self):
    return self.name


    then for the admin i have the following

    from django.contrib import admin

    # Register your models here.

    from .models import Problem,Deck

    class DeckAdmin(admin.ModelAdmin):
    filter_horizontal = ('problems',)

    admin.site.register(Deck, DeckAdmin)
    admin.site.register(Problem)


    and the admin looks like this [​IMG]

    well what i want to do is to have a custom filter to filter the available problems, the filter must be an interface where i can include and exclude tags associated with the problems, so i want to replace the filter search box with something like this

    [​IMG]

    so i can filter the problems by tags and then add then to the deck, how can i achieve that functionality?, i am new to django and have no idea how to proceed

    Continue reading...

Compartilhe esta Página