8

I have a Django web front-end that consumes data from a REST API backend. Even the users are made and created on the backend.

My Problem :

  • How to use 3rd party apps within this system, that heavily depend on django models/ORM ?
  • Is there something that can provide some bridge between the REST API resources and the ORM?

How can this problem be dealt with ?

Update

DRY principal seems to be failing in this situation.

Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197

3 Answers3

0

Probably things have changed since this question has been originally posted. Now there are a couple of interesting related questions on StackOverflow about this topic.

To code yourself a solution as explained in this answer, you can create an external service layer (aka services.py) and write there the logic to access the external resources. Your views will consume this layer, and make the proper actions. There are other questions that provide help on how to pass information from the original request received by the django-view to the external service like this or this

There is also a django app that takes this situation into account as explained in this answer. django-roa uses the Resource Oriented Architecture paradigm to solve this.

Francisco Puga
  • 23,869
  • 5
  • 48
  • 64
-2

I'm facing a similar obstacle with a new ecommerce project. The project is a front end to a full-fledged store management software (CMS+ERP+CRM). It needs to use the master product database, but have its own entries for product reviews, ratings and so on.

The initial thought was to make a cached copy of the master database. The website will benefit from fast loading times for the cached items, but the implementation is not trivial.

After some considerations, the selected approach was updating the website's DB from the management program. This way the website's copy will always be correct, and most of the implementation doesn't need to worry about REST services (it'll still be used for user registration, shipment tracking etc.)

In your case, where you can't have the service update your own database remotely, you need to come up with a mechanism that allows you to refer to REST recourses like regular models, and that caches them in the background.

Important note: research for a way to make sure the cache is always correct (non-dirty)...

Dor
  • 902
  • 4
  • 24
-3

I'm not sure I completely understand your question or requirements. The way I am reading it, you have a primary back-end which is basically a black-box, and you want to use some 3rd-party apps in your project which use the Django ORM.

I am unclear as to why there would be a need for being able to have a two-way synchronization between the two data-stores. The users of your project would be returned data from your primary back-end, and from the project's ORM.

Since you are concerned about saving the "ORM" data in your primary back-end, maybe you would consider creating a Transaction Middleware that would fire any time ORM data gets updated, which could serialize the structure being saved and transmit it to your REST API. This REST API, I assume, is able to accept arbitrary data structures?

You'll probably at least want to use some form of middleware, and maybe a utility module/class to help form the "bridge".

imm
  • 5,837
  • 1
  • 26
  • 32
  • Hm.. middleware comes into action before and after views. But DB calls are generally made in views, so how can a middleware do the magic ? – Yugal Jindle Feb 26 '12 at 07:46
  • @YugalJindle, use TransactionMiddleware. See https://docs.djangoproject.com/en/dev/topics/db/transactions/ too. – imm Feb 26 '12 at 07:50
  • Interesting.. I think that was helpful. Can you link to a project using similar bridging technique ? – Yugal Jindle Feb 26 '12 at 07:58
  • But.. can you link to a sample project using this technique, since I suppose this must be a standard problem. – Yugal Jindle Feb 26 '12 at 18:47
  • can you explain a bit more.. ? – Yugal Jindle Mar 02 '12 at 06:37
  • I understand what you suggested that how can I intercept the db calls, but my REST API is not a db table, so I don't have an idea how to map a `db` query to my `REST API`. Eg: `Entry.objects.filter(pub_date__year=2005).order_by('-pub_date', 'headline')` to my REST API which understands only URLs. That is why I asked if you know of a opensource project using similar stuff. If intercepting was the only case then my question would have been "How to intercept DB calls in django orm ?" – Yugal Jindle Mar 03 '12 at 04:27
  • Can you please clarify what your REST API is capable of accepting? Can it accept arbitrary data? – imm Mar 03 '12 at 15:47
  • Well, `REST API` is url interface to an object that provides CRUD to the object properties. – Yugal Jindle Mar 03 '12 at 16:31
  • Yes...but I assume its underlying data store won't precisely mimic whatever you have in your Django project, which is why you made the post in the first place. So if you can allocate an area of your underlying data store for unstructured data, that would be one option. – imm Mar 03 '12 at 16:40
  • It looks like a rocket science to me : Mimicing table joins with simple CRUD without some opensource project mature enough to handle this science over REST. So? – Yugal Jindle Mar 03 '12 at 17:02