Does Django call a REST API from models or views?

I need to call external REST APIs from Django. External data source schemas resemble my Django models. I have to keep synchronized remote and local data (maybe not relevant for the question)

Questions:

  • What is the most logical place to call external web services: from a model method or from a view?
  • Should I put the code that calls the remote API into external modules that will then be called by the views?
  • Is it possible to conditionally select a data source? The value of representing data from the REST API or local models depending on their "freshness"?

thanks

EDIT: for people who want to close this question: from the very beginning I posed the question in three simple questions, and so far I have received good answers, thanks.

+8
python rest django django-models django-views
source share
2 answers
  • I think this is an opinion where you can call web services. I would say do not pollute your models, because this means that you probably need instances of these models to call these web services. This may not make any sense. Your other choice is to do @classmethod things on models that are not very clean, I would say.

    A call from a view is probably more natural if accessing the view itself is what calls the web service call. It? You said that you need to synchronize the situation, which indicates the possible need for background processing. At this point, you can still use views if your background processes trigger HTTP requests, but often not the best design. Anyway, you probably need your own REST API for this, which will require a code branch from your average website look.

    My opinion is that these calls should be placed in modules and classes specially encapsulated for your remote calls and processing. This makes things flexible (background jobs, signals, etc.), and also easier to unit test. You can call this code in views or elsewhere, but the logic itself must be separated from both views and models in order to perfectly separate them.

    You should imagine that this logic should exist on its own if there was no Django around it, and then create other elements that connect this logic with Django (for example: model synchronization). In other words, keep things atomic.

  • Yes, the same reasons as above, especially flexibility. Is there a reason not to?

  • Yes, just create an equivalent interface. Approach each class map to the interface. If the fields are the same and you are lazy, in python you can simply discard the required fields as dicts to the constructor (using * *kwargs ) and do it or rename the keys using some kind of convection that you can handle. I usually create a simple data display class for this and process the django or rest models in the list comprehension, but it is not necessary if everything matches, as I already mentioned.

    Another related option is to include data in a common cache structure, such as Redis or Memcache. It might be wise to atomize this information if you are interested in "freshness." But overall, you should have one source of authority that can tell you what's really fresh. In situations of synchronization, I think it is better to choose one or the other, so that everything is predictable and understandable.

The last thing that can affect your design is that, by definition, synchronizing things is a complex process. Synchronization is usually very prone to failures, so you should have some kind of robust mechanism, such as a task queue or job system for retries. Always assume that calling a remote REST API that invokes calls can result in crazy reasons like network hicups. Also consider transactions and transactional synchronization behavior. Since they are important, he again points out that if you put all this logic directly into the view, you are likely to run into the problem of reusing it in the background, not being distracted from all this anyway.

+5
source share

What is the most logical place to call external web services: from a model method or from a view?

Ideally, your models should only talk to the database and not have a clue what is happening with your business logic.

Should I put the code that calls the remote API in external modules that will then be called by the views?

If you need to access them from several modules, then yes, placing them in a module makes sense. That way you can use them effectively.

Is it possible to conditionally select a data source? The value of representing data from the REST API or local models depending on their "freshness"?

Of course it is possible. You can simply implement a way to get your data on demand. But a more efficient way could be to simply avoid this logic and simply synchronize local data with remote data and show local data in views.

+3
source share

All Articles