Django - admin filter to "Add..." page (Model with more then one FK)

I am trying to filter the "Test" content according to "Plan" selection in Admin page as described in the attached picture. Please open this link to see the picture.

I am looking for an easy way to filter the content of "Test" drop list according to the selection of "Plan" drop list.

Thanks for your help, Eran

While my models looks like:

class TestPlan(models.Model):
    test_plan_name = models.CharField(max_length=200)
    def __str__(self):
        return self.test_plan_name

class Test(models.Model):
    test_plan = models.ForeignKey(TestPlan, on_delete=models.CASCADE)
    test_name = models.CharField(max_length=200)
    test_type = models.CharField(max_length=200)
    manual_ttc = models.IntegerField(default=0)
    priority = models.IntegerField(default=0)
    owner = models.CharField(max_length=200)
    drop_name = models.CharField(max_length=200)
    test_description = models.CharField(max_length=200)
    note = models.CharField(max_length=200)
    ac = models.CharField(max_length=200)
    def __str__(self):
        return self.test_name

class Result(models.Model):
    plan = models.ForeignKey(TestPlan, on_delete=models.CASCADE)
    test = models.ForeignKey(Test)
    status = models.CharField(max_length=100)
    version = models.CharField(max_length=100)
    bug = models.CharField(max_length=100)
    result_path = models.CharField(max_length=100)
    def __str__(self):
        return self.status

1 answer

  • answered 2018-01-14 11:30 at14

    Create a new change_form_template for the admin by extending the admin_change_form

    Write your own custom JS to handle the change event


    {% extends 'admin/change_form.html' %}
    {% load static %}
    {% block admin_change_form_document_ready %}
        {{ block.super }}
        <script type="text/javascript" src="{% static 'path_to_js.js' %}"></script>
    {% endblock %}

    create the JS file and add in your custom logic.

    in your, tell django to use your custom change_form html

    class ResultAdmin(admin.ModelAdmin):
        change_form_template = '<template_directory/your_change_form.html>'