Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
I am having some trouble designing API endpoint for my Django App (DRF)

I am designing an API for an E-learning project. The platform enables students to take online tests. The tests contain questions and questions can have multiple options. Below is the schema for my app:

Test:
    id: Primary Key
    duration: Integer
    created: Timestamp

Question:
    id: Primary Key
    serial: Integer
    test: Foreign Key (Test)
    body: Char Field

Option:
    id: Primary Key
    question: Foreign Key (Question)
    body: Char Field
    correct: Boolean Field

StudentSelectedOption:
    id: Primary Key
    question: Foreign Key (Question)
    student: Foreign Key (User)
    option: Foreign Key (Option)

Now, the problem is that I want to create an endpoint for returning student selected options based on the requesting user

/test/<int:test_id>/student-answers/

For example if any user wants to get their answers for test id 1, they will go to /test/1/student-answers/ (The user will be extracted from the request object, because I am using token authentication)

But I am not able to filter selected options related to the test id and user. Can someone help me out?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

To return a filtered queryset on DRF APIVIew, you can override get_queryset method like this :

Assume that you have already have a StudentSelectedOptionSerializer

from rest_framework.generics import ListAPIView

class StudentSelectedOptionListView(ListAPIView):
    serializer = StudentSelectedOptionSerializer

    def get_queryset(self):
        """
        This view should return a list of all answer of a specific test
        for the currently authenticated user.
        """

        user = self.request.user
        test_id = username = self.kwargs['test_id']
        queryset = StudentSelectedOption.objects.filter(question__test_id=test_id, student_id=user.id)
        return queryset

Note : You can traverse “relationship paths” using Django’s __ syntax to filter fields on a related model. EX: question__test_id in your case. More info on DRF filter. And Django filter trough foreign key

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda