0

I have a problem with a queryset in one view. My idea is show all users who are not registered in a program, I put here the models:

models.py

class UCAUser(AbstractUser):
    dni_cif=models.CharField(
        max_length=9,
        blank=True,
        verbose_name="DNI/CIF"
    )


class InscripcionRealizada(models.Model):
    formulario = models.ForeignKey(Formulario)
    inscrito = models.ForeignKey(UCAUser,related_name="inscripciones_realizadas")
    fecha_registro = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = "Inscripción realizada"
        verbose_name_plural = "Inscripciones realizadas"

    def __str__(self):
        return "{} - {} - {}".format(self.formulario.programa, self.formulario.edicion, self.inscrito)

You can see UCAUser and InscripcionRealizada are connected by InscripcionRealizada.inscrito field.

view.py

class InscribirUsuariosListView(ListView):
    template_name = "inscripciones/InscribirUsuariolist.html"
    model = UCAUser
    group_required = ['Administrador']
    login_url = "auth-login"

    def get_queryset(self):
        qs = super(InscribirUsuariosListView, self).get_queryset()
        return qs.filter(UCAUser.objects.filter(inscripciones_realizadas__formulario!=self.kwargs['formulario_id']))

    def get_context_data(self, **kwargs):
        context = super(InscribirUsuariosListView, self).get_context_data(**kwargs)
        context['formulario_id'] = self.kwargs['formulario_id']
        return context

When I try this, I get an error:

not enough values to unpack (expected 2, got 1)

Any idea?

demongolem
  • 9,474
  • 36
  • 90
  • 105
FangusK
  • 1
  • 1
  • 1
    I presume the error is due to `qs.filter(UCAUser.objects.filter...` in `get_queryset` but its not clear what you hope this to achieve – Sayse May 08 '17 at 07:43
  • 2
    The other visible issue is `inscripciones_realizadas__formulario!=self.kwargs['formulario_id']`. It isn't correct to use `!=` in a Django queryset. Use `exclude` instead. – ChidG May 08 '17 at 07:45
  • Adding to ChildG comment, take a look here: http://stackoverflow.com/questions/687295/how-do-i-do-a-not-equal-in-django-queryset-filtering/4139956#4139956 – nik_m May 08 '17 at 07:46

0 Answers0