Display data from external service in Django admin TabularInline

I have next django code

models.py

class Service(models.Model):
    username = models.CharField(...)
    password = models.CharField(...)
    ...

class Order(models.Model):
    service = models.ForeignKey('Service', on_delete=models.CASCADE, related_name='orders')
    number = models.CharField(...)
    creation_date = models.DateTimeField(...)
    ...

admin.py

@register(Service)
class ServiceAdmin(ModelAdmin):

    class OrderNewInline(TabularInline):
        '''
        Display orders from remote service, external api, data-file ...
        List of orders to be imported
        '''
        model = Order
        extra = 0
        can_delete = False

        def has_change_permission(self, request, obj=None):
            return False

        def has_add_permission(self, request, obj=None):
            return False

        # Getting orders from remote service, external api, data-file ...
        # @param Service service - current service model instance
        # @return list - list of external orders
        def get_data(self, service, ...):
            ...

    class OrderInline(TabularInline):
        '''
        Display orders already imported
        '''
        model = Order
        ...

    inlines = [OrderNewInline, OrderInline]

Please help me find solutions to the following questions:

  1. How to display orders in OrderNewInline from remote service, external api, data-file …
  2. Create column in OrderNewInline with checkbox for mark orders have to be imported
  3. Add a button to OrderNewInline form to trigger orders import event
  1. Different answers depending upon the source. Each one needs to be handled individually. Also, are you talking about providing “edit” facilities for these external references, or just for reference display? (The later is significantly easier than the former.)

  2. It really looks to me like you’re trying to extend the admin facility beyond what it’s intended to do. It’s easy to get to the point that you’re going to be a lot better off creating your own custom views to do this rather than push the admin to do things it was never intended to do - especially if there are going to be non-admin people trying to use it.

Ken

Ken, thank you for the answer!

  1. Data source is external website which has json-api interface. Service model store connection information for json-api client. There are several accounts on website each account may have Order-s. Opening for edit Service model in Django admin I want to see list of orders on external website for this Service which is not imported yet (OrderNewInline), and orders already imported (OrderInline). On OrderNewInline form I need column with checkbox for selecting orders to be imported to my database, and button to run importing action.

  2. I think to override method which getting data (Orders instances) for OrderNewInline “get_queryset” or any other. But I can’t find how to convert list of Orders to QuerySet.

You can’t create a QuerySet from a list. A QuerySet is a representation of the results of an SQL query having been issued to a database. If what you really need in that position is a QuerySet, then the only solution I can think of is to import that external resource into a local table and query it. Otherwise, it just becomes a set of choices from which you can make selections to add as rows to your table.

Hello Ken,

How can we display data for reference from external API on aour admin panel. Is their any way to do so?

Hello matias,

Did you find any convincing solution for this? I am also looking to solve similar problem.

Personally, my opinion is that it’s not worth the effort to try.

If you’ve got a non-standard mechanism for accessing the database, I think you’re going to find it significantly easier to build your own admin for it. (That’s not to say that you can’t leverage large parts of the admin while doing so - you don’t really need to start from scratch. But the Admin itself really isn’t suited for doing that.)