Question

I'have the following ORM Query set, it works properly but now i need it to exclude itself from appearing to a user who already added a row with the same id on the table.

In other words i want the user not to answer the question twice if the user already added an answer to that specific question he will never see the values. here is my query ORM

right_now = datetime.datetime.now() - timedelta(minutes=-10)
partidos = encuesta.objects.filter(fecha__gt=right_now).order_by('fecha')

this ORM Query above shows all the Questions from today untill 10 minutes before the due date.

Now i want show only the questions that a specific user has already answered. as you can see below, i need only to appear those questions he has not answered yet but still being filtered by the date and same order.

Any ideas?

enter image description here

Here is my model.

models.py

class equipo(models.Model):

nombre = models.CharField(max_length=30)
bandera = StdImageField(upload_to='bandera/%Y/%m/%d',
                        variations={
                        'large':(53,53, False),
                        'thumbnail': (70, 26, False)})

GRUPOS = (
    ('A', 'Grupo A'),
    ('B', 'Grupo B'),
    ('C', 'Gropo C'),
    ('D', 'Gropo D'),
    ('E', 'Gropo E'),
    ('F', 'Gropo F'),
    ('G', 'Gropo G'),
    ('H', 'Gropo H'),
)

grupo = models.CharField(max_length=1, choices=GRUPOS)

def banderaEquipo(self):
    return '<img src="/media/%s">' % (self.bandera.thumbnail)

banderaEquipo.allow_tags = True

def __unicode__(self):
    return self.nombre


class encuesta(models.Model):

equipoA = models.ForeignKey(equipo, related_name='equipo_equipoA')
golesEquipoA = models.IntegerField(max_length=2, null=True, blank=True)
equipoB = models.ForeignKey(equipo, related_name='equipo_equipoB')
golesEquipoB = models.IntegerField(max_length=2, null=True, blank=True)

ETAPA = (
    ('1', 'Primera Etapa'),
    ('2', 'Octavos De Final'),
    ('3', 'Cuartos De Final'),
    ('4', 'Semifinal'),
    ('5', 'Final'),
    ('6', '3ra Posicion')
)

etapa = models.CharField(max_length=1, choices=ETAPA)
fecha = models.DateTimeField(auto_now_add=False)

def __unicode__(self):

    return "%s Vs. %s" % (unicode(self.equipoA), unicode(self.equipoB))


class respuesta(models.Model):

encuesta = models.ForeignKey(encuesta)
empresa = models.ForeignKey(empresa)
empleado = models.ForeignKey(empleado)
equipoA = models.IntegerField(max_length=1)
equipoB = models.IntegerField(max_length=1)
fecha = models.DateField(auto_now_add=True)

def __unicode__(self):

    return "%s" % (unicode(self.encuesta))
Was it helpful?

Solution

Main idea:

Question.objects.exclude(answers__user=user)

In your case (if you add related_name encuestas to respuesta FK field encuesta):

encuesta.objects.exclude(respuesta__empleado=user).filter(fecha__gt=right_now).order_by('fecha')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top