简体   繁体   中英

Django auto-populate on admin load

I am building a simple Django project to manage a bunch of phone numbers and call logs that we have in Twilio and other cloud telephony providers. I would like to keep in Django a list of the phone numbers that we have in Twilio. I am easily able to query Twilio's API to get our current numbers, and it seems quite simple to use this list to populate my Django DB.

However, to make sure the list in Django is kept in sync with Twilio, I would like to call Twilio's API and update the list of numbers in Django every time a user loads the list of phone numbers in the admin.

Question: how do I get Django to run this call every time the list of numbers loads in the admin?

You could use a custom Django admin class and redefine eg ModelAdmin.get_queryset to fetch the new data, then return the QuerySet:

class MyModelAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        update_data_from_api()
        return = super(MyModelAdmin, self).get_queryset(request)

一种方法是将自定义admin操作添加到电话列表的ModelAdmin中以进行上载过程,然后在此处启动API提取。

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