简体   繁体   中英

Access other model from django admin

I'm writing a tool that monitors an external api and verifies that it works as documented.

Basically I have two models:

ServerTest , which has a url and a string of json that we expect the target server to return for that url

and

TestResult which has a foreign key relationship with ServerTest , a string containing the server's actual response, and a timestamp for when the test occurred.

Periodically, the app will run through each ServerTest , ping the TargetServer, and create a new TestResult instance for each ServerTest .

Now what I want to do is view a json diff between the actual and expected json. I was thinking I might use this widget: http://tlrobinson.net/projects/javascript-fun/jsondiff/ . I was also thinking I might embed that widget in the django admin. The problem is, in order to generate the diff I need to access both the ServerTest instance and the latest corresponding TestResult instance. It doesn't seem like there's a straight-forward way to do this through django admin. Should I give up and just make a separate view? I'd rather not do that, because I feel like I would keep adding functionality to that view until I had reimplemented half of the django admin interface.

To recap I need to:

1) Access a ServerTest instance

2) Access a TestResult instance

3) Add an html container (say, on the ServerTest admin page)

4) Run some javascript that has access to the currently viewed ServerTest's expected_json and the latest corresponding TestResult's actual_json

5) Have that javascript dynamically inject html into the aforementioned container

Since you're injecting html anyway, i would do a simple view, that generates diff you need, and then call it inside admin page.

In order to add custom JS in admin, you can use ModelAdmin Media

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM