Opened 4 years ago

Closed 4 years ago

#28476 closed Bug (invalid)

Difficulty getting filter_horizontal to extend to the InlineModelAdmin object in admin?

Reported by: bbiney1@swarthmore.edu Owned by: nobody
Component: contrib.admin Version: 1.11
Severity: Normal Keywords: inlinemodeladmin admin
Cc: bbiney1@swarthmore.edu Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm kind of surprised at how I haven't been able to find an example or very much discussion of someone applying filter_horizontal to an InlineModelAdmin object. I did see one report from 9 or so years ago of someone acknowledging a bug wit the feature, but that was resolved. I'm posting this ticket to simply ask again because at this point i really don't know how I'm supposed to use this really helpful feature.

For full context, I have two models. One called Statement and one called Keyword. Each Keyword object has a m2m with a Statement, so a statement can have many keywords. I would like to, in the admin change page for statements, give the user the ability to add or remove keywords from a statement. So inlines are the obvious answer.

But there are a lot of Keyword objects, so the drop down scrollable menu that is automatically rendered by django isn't usable for me. Filter_horizontal would be ideal.

But I've been trying to get this to work and it seems like I just can't find the syntax to do it. Here's my code block for both of the models and their presentation in admin.

@python_2_unicode_compatible
class Keyword(models.Model):
    word      = models.CharField(max_length=200)
    statement = models.ManyToManyField(Statement)
    def __str__(self):
        return self.word

@python_2_unicode_compatible
class Statement(models.Model):
    statement_id = models.CharField(max_length=200)
    title = models.CharField(max_length=200)
    issue_date = models.DateField("Issue-Date")
    author = models.ForeignKey(Person)
    released_by = models.ForeignKey(Organization)
    keywords = models.ManyToManyField('KeywordInContext')

class KeywordInline(admin.TabularInline):
    model = Keyword.statement.through

class KeywordAdmin(admin.ModelAdmin):
    filter_horizontal = ('statement',)

class StatementAdmin(admin.ModelAdmin):
    inlines = [ KeywordInline,]

Change History (3)

comment:1 Changed 4 years ago by bbiney1@swarthmore.edu

Cc: bbiney1@swarthmore.edu added

comment:2 Changed 4 years ago by bbiney1@swarthmore.edu

Triage Stage: UnreviewedAccepted

comment:3 Changed 4 years ago by Tim Graham

Resolution: invalid
Status: newclosed

Please see TicketClosingReasons/UseSupportChannels for places to ask usage questions like "I just can't find the syntax to do it."

By the way, accepting your own ticket isn't the proper workflow for bugs; see Triaging tickets. Thanks.

Note: See TracTickets for help on using tickets.
Back to Top