If statement does not execute as I expected in Django

0

I have the following function:

def destroy(self, request, *args, **kwargs):
    id_act = kwargs.get('pk', None)
    userObject = User.objects.get(pk=self.request.user.pk)
    try:
        ocurrencia = Ocurrencia.objects.get(pk=id_act)
        if ComprobarPermisos.es_creador(ocurrencia, userObject) or ComprobarPermisos.es_jefe(ocurrencia,
                                                                                             userObject):
            repeticion = Repeticion.objects.get(ocurrencia=ocurrencia)
            if repeticion.__isnull== True:
                self.perform_destroy(ocurrencia)
                return Response({'Actividad eliminada'}, status=status.HTTP_301_MOVED_PERMANENTLY)
            else:
                evento = ocurrencia.event
                Ocurrencia.objects.filter(event_id=evento.pk).delete()
                self.perform_destroy(evento)
                return Response({'Actividad eliminada'}, status=status.HTTP_301_MOVED_PERMANENTLY)
        else:
            return Response({'Usted no puede ejecutar esta operacion'}, status=HTTP_403_FORBIDDEN)
    except Http404:
        return Response({'Not Found'}, status=status.HTTP_404_NOT_FOUND)

My problem is as follows: I am doing a test to test this function and my "scenario" is that repeticion = Repeticion.objects.get(ocurrencia=ocurrencia) fails. That is, if there is no repetition, what is coming in the else must be executed. When I run the test, the if fails but does not execute what comes in the else. Any idea what may be happening?

    
asked by Ethan 17.11.2017 в 17:29
source

1 answer

1

In the event that Repeticion.objects.get (occurrence = occurrence) fails, a "null" object does not return an error DoesNotExist returns.

Surely what is running is the except and being limited to Http404 simply continues the execution.

Try this:

try:
    go = Repeticion.objects.get(ocurrencia=ocurrencia)
    //codigo si ha encontrado el registro
except Repeticion.DoesNotExist:
    //codigo si NO ha encontrado el registro
    
answered by 21.11.2017 / 12:41
source