Accessing to objects of a dynamic way in django template

149 views
Skip to first unread message

Bernardo Garcia

unread,
Dec 4, 2016, 7:12:17 PM12/4/16
to Django users

I have the following class based view in which I perform to queryset:


class PatientDetail(LoginRequiredMixin, DetailView):

   model = PatientProfile

   template_name = 'patient_detail.html'

   context_object_name = 'patientdetail'

   

   def get_context_data(self, **kwargs):

       context=super(PatientDetail, self).get_context_data(**kwargs)

       queryset= RehabilitationSession.objects.filter(patient__slug=self.kwargs['slug'])                 
context.update({'patient_session_data': queryset,})
return context

When I acces the value of patient_session_data key sent, in my template:

{% extends "base.html" %}

{% block content %}

{{patient_session_data}}

{% endblock content %}


I get this three QuerySet objects

<QuerySet [<RehabilitationSession: Hernando Zambrano Cuellar>, <RehabilitationSession: Hernando Zambrano Cuellar>, <RehabilitationSession: Hernando Zambrano Cuellar>]>



I want access to specific attibute named upper_extremity of my RehabilitationSession model, then I make this:


{% for upperlimb in patient_session_data %}

{{upperlimb.upper_extremity}}

{%endfor%}


And I get this in my template:

Izquierda Izquierda Izquierda

This mean, three times the value, because my queryset return me three objects. This is logic.

For access to value of a separate way I make this in my template:

{{patient_session_data.0.upper_extremity}}

And I get:

Izquierda


My goal

I unknown the amount of querysets objects RehabilitationSession that will be returned by the queryset executed in my PatientDetail

 cbv, because the number is dynamic, may be any amount of objects returned.


I want read the value content of each patient_session_data upper_extremity and accord to the value make something in my template,

But I want read it of a dynamic way,without use {{patient_session_data.<0-1-2-3>.upper_extremity}}


For example in a hypotetical case:


#if all objects returned have same value in upper_extremity

{% if patient_session_data.upper_extremity == 'Izquierda' %}

    <div class="col-md-10">

          <h5>Segmentos corporales a tratar</h5>

          <small>Codo - mano - falange</small>

    </div>

{%endif%}



I think that I have count the amount of objects and make with them some actions, because I don't have clear ... 


How to can I access of a individual way to the objects returned of a dynamic way without matter the objects number returned?

ludovic coues

unread,
Dec 5, 2016, 3:52:22 AM12/5/16
to django...@googlegroups.com
You could write a custom filter to apply on the queryset.
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/c17e25b2-cbce-4401-8065-9704054648e7%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42
Reply all
Reply to author
Forward
0 new messages