Home > Mobile >  Checking for data in the database and re-requesting a third-party API
Checking for data in the database and re-requesting a third-party API

Time:05-05

I'm using Django Rest Framework for the following task (I'll write it in detail so that everything is clear):

  1. The service must implement a REST API that accepts POST requests as input with content like {"questions_num": integer} .

This item is done.

  1. After receiving the request, the service, in turn, requests the number of questions specified in the received request from the public API (English questions for quizzes) https://jservice.io/api/random?count=questions_num.

This is also done.

  1. Further, the received answers should be stored in the database (or Postgres or Sqlite) fields from the quiz: 1. Question ID, 2. Question text, 3. Answer text, 4. - Question creation date.

This is also done.

  1. And it's not clear how to normally do the following moment:

In the event that the database has the same question, additional requests must be made to the public API with quizzes until a unique question for the quiz is obtained.

It turned out to be a crutch, but I would like it to be beautiful, or at least normal.

Tell me how to implement this?

Thanks in advance.

CodePudding user response:

I found how to solve this problem myself:

User.objects.filter(id__in=[1, 5, 34, 567, 229])

print _.query
SELECT <fields> FROM "auth_user" WHERE "auth_user"."id" IN (1, 5, 34, 567, 229)

In my case, the following happened:

Question.objects.filter(id_question__in=id_question).count()

CodePudding user response:

Please show the whole code so you can see the full picture

  • Related