Is it possible to filter the selection on an external key in the Django admin panel?

Here is my situation:
I have three models: class, course and program. The class has a foreign key so that the course and course have a foreign key for the Program.

Here is my problem:
When I need to add some grades to my admin panel, I have a complete list of courses. The first problem: one course name can be found in two different programs, and it is difficult to identify in the list. This is currently shown as Program.name - Course.name

I was wondering if there is any solution that can help me filter out the list of courses by program when I want to create or change a class. (I'm talking about an interface for creating a class, not a list in front of this interface).

EDIT:
Thanks for your answers. I think I will have to encode my own widget, making my filter a client side with AJAX.

+3
source share
2 answers

You can write such a filter using AJAX (using jQuery or a similar structure would be the easiest way to do this). You will create a custom form widget with two lists. The first will be filled with all available programs by the server when the page loads. Your javascript will wait for a choice, and then ask the server (this is part of AJAX) for the list of courses in this program and update the second list accordingly.

, , ; , jammon answer ( , . Django ). , , , , , .

Django - admin.py. , - :

from django.contrib import admin
from myapp.models import Program, Course, Grade

admin.site.register(Program)
admin.site.register(Course)
admin.site.register(Grade)

. -, :

class GradeInline(admin.TabularInline):
    model = Grade
    extra = 1

, admin.TabularInline admin.StackedInline. TabularInline, , . extra , .

, :

class CourseAdmin(admin.ModelAdmin):
    model = Course
    inlines = (GradeInline,)

, , . , . , admin.py :

from django.contrib import admin
from myapp.models import Program, Course, Grade

class GradeInline(admin.TabularInline):
    model = Grade
    extra = 1

class CourseAdmin(admin.ModelAdmin):
    model = Course
    inlines = (GradeInline,)

admin.site.register(Program)
admin.site.register(Course, CourseAdmin)
0

, , :

InlineModelAdmin ModelAdmin.

class CourseAdmin(admin.ModelAdmin):
    class GradeInline(admin.StackedInline):
        model = Grade
        fk_name = 'course'
        extra = 2
        and so on

change_view , , .

, , course.__unicode__, - "grade.name(program.name)".

0

All Articles