Filtering a second admin dropdown's contents based on the first dropdown's selection

9 views
Skip to first unread message

Sithembewena Lloyd Dube

unread,
Mar 24, 2012, 12:23:47 AM3/24/12
to django...@googlegroups.com
Hi everyone,

I have two dropdowns on the create/ edit pages of an object in the admin site. I need to filter the second dropdown's contents based on the selection in the first (as a user-friendliness measure). I have tried the recommended use of formfield_or_foreignkey with no progress. The difference is that the following example uses the request object's user, whereas I need a way to obtain and use the first dropdown's selected item.

I also noticed that the form does not post when a selection is made.

class MyModelAdmin(admin.ModelAdmin):
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "car":
            kwargs["queryset"] = Car.objects.filter(owner=request.user)
            return db_field.formfield(**kwargs)
        return super(MyModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

Any ideas?

--
Regards,
Sithembewena Lloyd Dube
Reply all
Reply to author
Forward
0 new messages