Which is the best solution in DRF sending an ID of primary key of documents or sending ID with the name of document containing the URL?

Question:

So, there is an article API containing different parameters including documents model parameter.

Documents model is connected to article model using many to many field.

The current data that is coming is this:

[
    {
        "id": 2,
        "headline": "Article 2",
        "abstract": "Abstract 2",
        "content": "Content 2",
        "published": "2022-08-10T11:28:04.351030Z",
        "status": true,
        "get_tags": [
            {
                "id": 1,
                "tag": "Python"
            },
            {
                "id": 2,
                "tag": "Django Rest Framework"
            }
        ],
        "file_documents": [
            {
                "id": 2,
                "document": "http://127.0.0.1:8000/images/tags-Many_RMVHBid.png"
            }
        ],
        "created_by": "dummyTest"
    }
]

Now here what I want to ask is:

"file_documents": [
            {
                1
            }

If send instead a primarykey instead of whole documents like above. will the data can still be fetched at the frontend and also which one is better this method or the one above this?

Asked By: Abdullah Roshan

||

Answers:

If the server just sends the ID, the frontend will need to get the document information using that ID.

So in the frontend, there are two ways to get the detail information of the document using the ID.

  1. you can store all the documents in the frontend, and find the document with that ID without using other APIs.
  2. you can call other API to get the document data with that ID

The above two methods are all ineffective.
Getting all the related data at once is much more effective and faster.

Answered By: Metalgear