I implement it in the rest-framework project and I need tips for understanding it more.

It is clear that the rest-framework is necessary for building an API and working with external consumers (developers, other services). But wonders whether it is possible to use the data that the framework gives for internal interactions.

For example, before rest implementation, object detailing is available at the link:

element/<element_id> 

which corresponds to the view:

 class ElementDetailView(DetailView): model = Element template = "element-detail.html" .... <какие-то переопределенные методы родителя> 

after rest implementation, object data is also available by reference.

 api/v1/element/<element_id> 

and actually the question is, can parts of my project exchange data using the rest-framework?

For example, if I want to implement ajax-loading of object data, I will add the following code somewhere in the view:

 class ElementDetailView(DetailView): model = Element template = "element-detail.html" .... <какие-то переопределенные методы родителя> .... if request.is_ajax(): return json-object 

accordingly, the url in the script will refer to this view, and the resulting data will be processed by the script.

but in the case of the rest-framework, can I use the url from rest-api in the script?

 api/v1/element/<element_id> 

will it be right? or for internal interactions use only internal classes and functions, and rest only for external interactions?

    1 answer 1

    For example, if I want to implement ajax-loading of object data, I will add the following code somewhere in the view ...

    ... but in the case of the rest-framework, can I use the url from the rest-api in the script?

    That’s what I’m trying to do lately, getting rid of the constructions of the form:

     if request.is_ajax(): return json-object 

    Which with the proliferation and complexity of the project becomes difficult to maintain.

    I often use DRF in my projects for internal needs, there is nothing wrong with that.

    • Thanks, that is, as I understand it, we can use drf wherever the data returned corresponds to the expectations? in fact, we can replace the DetailView with the result of drf drilldown, only customize the template (provided that there is nothing on the page except for drilldown)? - while1pass
    • I don’t know about html returns, I didn’t use DRF like that. I meant the case when you need to get data in the form of json, for example, when some widget on the page requests it. Ie the usual view gives the HTML page and if on this page you need some kind of data in the form of json then we take it from DRF. - mks
    • for example, if you need to give a json object with a truncated field composition (for different widgets). It is inconvenient to fence this logic in the code of the main view. - mks
    • that is, for each object of a certain type, will we write our own serializer? - while1pass
    • Yes, it often happens that several sets of serializers are written on one object in different situations. For example, to receive an order with an embedded user, or an order only with its id. - mks