'ReverseManyToOneDescriptor' object has no attribute 'form'

0

Greetings, my question is this: I need to filter all users who are not registered in a certain form (whose id comes from the previous template with a kwarg), I pose the models:

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)

and the view:

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.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

As you can see, the relation between UCAUser e InscripcionRealizada is by Foreignkey , but when I try to do the filter in the view I get this error:

'ReverseManyToOneDescriptor' object has no attribute 'formulario' 

Any ideas?

Thank you.

    
asked by FangusK 03.05.2017 в 10:16
source

1 answer

0

I already managed to solve it, thanks for your help. The failures were the ones suggested:

  • First of all I was doing the queryset wrong, because I was putting the model back into the search, without taking into account that queryset was already looking for me in the model.
  • Then to filter as I needed I had to put it as exclude.
  • I'll give you the solution in case it helps.

    Views.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.exclude(inscripciones_realizadas__formulario__id=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
    
        
    answered by 12.05.2017 / 11:56
    source