Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin using RESTful API v.s. Database

This is a bit of a strange question, I know, but bear with me. We've developed a RESTful platform using Python for one of our iPhone apps. The webapp version has been built using Django, which makes use of this API as well. We were thinking it would be a great idea to use Django's built-in control panel capabilities to help manage the data.

This itself isn't the issue. The problem is that everyone has decided it would be best of the admin center was essentially a client that sits on top of the RESTful platform.

So, my question is, is there a way to manipulate the model layer of Django to access our API directly, rather than communicated directly with the database? The model layer would act as the client passing requests and responses to and from the admin center.

I'm sure this is possible, but I'm not so sure as to where I would start. Any input?

like image 379
Wilhelm Murdoch Avatar asked Jan 26 '12 01:01

Wilhelm Murdoch


1 Answers

I remember I once thought about doing such thing. At the time, I created a custom Manager using a custom QuerySet. And I overrode some methods such as _filter_or_exclude(), count(), exists(), select_related(), ... and added some properties. It took less than a week to become a total mess that had probably no chance to work one day. So I immediately stopped everything and found a more suitable solution.

If I had to do it once again, I would take a long time to consider alternatives. And if it really sounds like the best thing to do, I'd probably create a custom database backend. This backend would, rather than converting Django ORM queries to SQL queries, convert them to HTTP requests.

To do so, I think the best starting point would be to get familiar with django source code concerning database backends.

I also think there are some important things to consider before starting such development:

  • Is the API able to handle any Django ORM request? Put another way: Will any Django ORM query be translatable to an API request?
  • If not, may "untranslatable" queries be safely ignored? For instance, an ORDER BY clause might be safe to ignore. While a GROUP BY clause is very unlikely to be safely dismissed.
  • If some queries can't be neither translated nor ignored, may them be reasonably emulated. For instance, if your API does not support a COUNT() operation, you could emulate it by getting the whole data and count it in python with len(), but is this reasonable?
  • If they are still some queries that you won't be able to handle (which is more than likely): Are all "common" queries (in this case, all queries potentially used by Django Admin) covered and will it be possible to upgrade the API if an uncovered case is discovered lately or is introduced in a future version of Django?

According to the use case, there are probably tons of other considerations to take, such as:

  • the integrity of the data
  • support of transactions
  • the timing of a query which will be probably much higher than just querying a local (or even remote) database.
like image 136
Antoine Pinsard Avatar answered Sep 21 '22 05:09

Antoine Pinsard