Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda