0

I am new to the Django framework. I want to create generic controller which can accept body as follows

{
    "operation":"select",
    "fields":["user_id","user_name","user_email"],
    "table":"user",
    "where":[        
            {
                "field":"user_id",
                "value":"1234",
                "operator":"=",
                "endwith":null
            }

    ]
}

produce a proper SQL query and generate proper JSON output. there is any way to do this using Django rest framework?

  • 1
    there is no *built-in way in DRF* to do this. – JPG May 15 '20 at 05:09
  • @ArakkalAbu is this approach is correct? – dhinchak developer May 15 '20 at 05:20
  • 1
    CURD is a food DRF cant make it :D neither anyting built in exists in Django to perform something like this. But you can do it easily. Parse your request use a mysql driver format the query get the result in a dict and your can serialize it simply passing in in `JsonResponse` – Yugandhar Chaudhari May 15 '20 at 06:09

1 Answers1

1

You can define your own API view and do all the logic in it. There is no built-in way to do this.

To your second question - honestly, it seems very strange to me to take a REST framework and do everything that controversy with the REST paradigm. For example - table name and operations supposed to be a particular URL and particular HTTP method in REST.

If you want to do it as in the question, I can suggest looking into GraphQL and corresponding Django packages:

Some basics about REST and how it should be done:

Igor Belkov
  • 446
  • 3
  • 8