Filter by a queryset with 2 kwargs

0

I have a URL where I pass 2 parameters:

Edicionlist.hmtl

 <a class="btn btn-primary btn-xs" href="{% url "inscripcion:inscripciones_personas_edicion-list" programa=element.id edicion=element.edicion%}">
 <span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> Ver inscritos
 </a>

urls.py

urlpatterns = [
url(r'^$', views.InscripcionesIndexView.as_view(), name="index"),

url(r'^inscripcion_personas_edicion/(?P<programa>\d+)/(?P<edicion>\d+)/$', views.InscripcionesPorEdicionListView.as_view(), name="inscripciones_personas_edicion-list"),
]

Well, now I want to filter the results of the model for those 2 fields:

views.py

class InscripcionesPorEdicionListView(ListView):
template_name = "appinscripciones/inscripciones/Inscripcioneslist.html"
model = Inscritos
group_required = ['Básico', 'Administrador']
login_url = "auth-login"

def get_queryset(self):
    qs = super(InscripcionesPorEdicionListView, self).get_queryset()
    return qs.filter(programa__exact=self.kwargs['programa']).filter(edicion__exact=self.kwargs['edicion'])

But I only filtered through the first one, which would be a program. Could someone tell me what I'm doing wrong in Queryset ?

Thank you very much.

    
asked by FangusK 20.12.2016 в 12:31
source

1 answer

0

Well, the only thing I see in your question is the following:

In your template:

...
<a class="btn btn-primary btn-xs" href="{% url "inscripcion:inscripciones_personas_edicion-list" programa=element.id edicion=element.edicion%}">
...

You must change to integers, so you must put the id, because in your urls.py you are looking for a regular expression with \d+ , so your url only searches for numbers, so change it to:

...
<a class="btn btn-primary btn-xs" href="{% url "inscripcion:inscripciones_personas_edicion-list" programa=element.id edicion=element.edicion.id %}">
...

Now, in your method get_queryset() I see unnecessary things, among those, make a filter for an object with lookup __exact , since by default that is the one used by django, so it is the same write Model.objects.filter(nombre__exact=a) and this Model.objects.filter(nombre=a) . Second, if you are sending an id in the url, you should search and filter from that id.

So your method would look something like this:

...
def get_queryset(self):
    qs = super(InscripcionesPorEdicionListView, self).get_queryset()
    programa = get_object_or_404(Programa, id=self.kwargs.get('programa', 0))
    edicion = get_object_or_404(Edicion, id=self.kwargs.get('edicion', 0))
    return qs.filter(programa=programa, edicion=edicion)
...

This would be to ensure that the program and the edition exist, but to reduce the number of queries you can do so:

...
def get_queryset(self):
    qs = super(InscripcionesPorEdicionListView, self).get_queryset()
    programa_id = self.kwargs.get('programa', 0)
    edicion_id = self.kwargs.get('edicion', 0)
    return qs.filter(programa_id=programa, edicion_id=edicion)
...

I hope I have helped you, any questions, comment:)

    
answered by 22.12.2016 / 15:42
source