I have the following model:
class Actividad(models.Model):
nombre = models.TextField()
es_principal = model.BooleanField()
fecha_inicio = DateField()
fecha_fin = DateField()
dirigente = TextField()
And this is your viewset:
class ActividadViewset(viewsets.ModelViewSet):
serializer_class = ActividadSerializer
queryset = Actividad.objects.all()
So far so good, it's a% co_of basic% of viewset
and when a request is made GET I return all activities.
Now I need to filter the activities from the data that comes in the request.data . For example, if what you are asking for is the activities according to the name or start date:
def get_queryset(self):
queryset = Actividad.objects.all()
nombre = self.request.query_params.get('nombre')
fecha_inicio = self.request.query_params.get('fecha_inicio')
if nombre:
queryset = queryset.filter('nombre' = nombre)
elif:
queryset = queryset.filter('fecha_inicio' = fecha_inicio)
return queryset
My question is how to build the Django Restframework
for several cases.
For example:
- Return activities according to name (ready)
- Return activities according to start date (ready)
- Return activities according to name, start date and end date
- Return activities according to name, start date, end date and if it is main
- Return activities according to start date, end date and if it is main
- Return activities according to start date and end date
In another language I would have used a queryset
but that does not exist in switch-case
. I have to do a if for each of the conditions in the previous list or is there a "dynamic" way to build the Python
? Thanks in advance.