Show only the first element of a list in templates

1

I have a table in which I show an object of sight, these are vaccines, what I want is that, for example, if they already put that vaccine, I will not get the name of the vaccine, only the dates in those who got the vaccines.

template.html

<table width="100%" class="table-bordered">
  <caption style="text-align: center; font-weight:bolder; font-size: 20px; color:black">Vacunas aplicadas</caption>
</td>
    <td>Descripción</td>
    <td>Fecha de aplicación</td>
    <td>Nota</td>
</tr>
{% for vaccine in preventive %}
    <tr>
        <td>{{ forloop.counter }}</td>
        <td>{{ vaccine.vaccines }}</td>
        <td>{{ vaccine.vaccine_date }}</td>
        <td>{{ vaccine.vaccine_note}}</td>
        {% ifchanged vaccine.vaccines%}
    </tr>
{% endfor %}

view.py

class PreventiveMedicineView(FormView):
    template_name = 'clipboard_medicinapreventiva.html'
    form_class = PrevMedicineForm

    def form_valid(self, form, **kwargs):
        vac = self.request.POST['description']
        vaccines = Vaccines.objects.get(description=vac)
        vaccine_date = self.request.POST['vaccine_date']
        vaccine_note = self.request.POST['vaccine_note']

        if not vaccine_date:
            vaccine_date=datetime.datetime.now().date()

        patient=Patient.objects.get(pk=self.kwargs['id'])
        clipboard_patient = Clipboard.objects.get(patient=patient)


        prev_medicine = PreventiveMedicine(clipboard=clipboard_patient,
                                           vaccines=vaccines,
                                           vaccine_date=vaccine_date,
                                           vaccine_note=vaccine_note)
        prev_medicine.save()
        success_url = reverse('preventive_medicine', kwargs={'id': patient.pk} )
        return HttpResponseRedirect(success_url)

    def form_invalid(self, form, **kwargs):
        print form._errors
        return self.render_to_response(self.get_context_data(form=form))

    def get_context_data(self, **kwargs):
        context = super(PreventiveMedicineView, self).get_context_data(**kwargs)
        context.update({
            'patient': Patient.objects.get(pk=self.kwargs['id']),
            'prev_medicine': PreventiveMedicine.objects.all(),
            'vaccines': Vaccines.objects.all(),
            'preventive': PreventiveMedicine.objects.filter(clipboard__patient=self.kwargs['id'])
        })
        return context

For example this form shows them to me like this:

tetanos--2016-01-02--test

tetanos--2015-10-03--descripcion.

H1N1   --2016-09-11--observaciones

hepatitis-2016-08-01--C 

Well, as I said above, what I want is that in the previous example, I'll show it to me like this

TETANOS

     --2016-01-02--test.

     --2015-10-03--descripcion.

H1N1   --2016-09-11--observaciones

hepatitis-2016-08-01--C 
    
asked by Mac Alexander 09.11.2016 в 20:01
source

2 answers

1

It is not clear to me what is the criterion to know if they have already given a vaccine or not, but in any case, my advice is to define it either in the view or in an Object Manager of the vaccine model and check the fulfillment of this criterion with a {% if%} in the template. For example, if there is a "set" attribute that is a Vaccine boolean, then you can say in the template:

{% for vaccine in preventive %}
<tr>
    <td>{{ forloop.counter }}</td>
    <td>
        {% if not vaccine.puesta %}
            {{ vaccine.vaccines }}
        {% endif %}
    </td>
    <td>{{ vaccine.vaccine_date }}</td>
    <td>{{ vaccine.vaccine_note}}</td>
    {% ifchanged vaccine.vaccines%}
</tr>
{% endfor %}

I hope I helped you. Regards!     Erick

    
answered by 24.01.2017 в 17:49
1

I can think of a simple solution using property as follows:

First, suppose you have the following two models:

class Vacuna(models.Model):
    nombre = models.CharField(max_length=30)

    def obtener_datos(self):
        return DatosVacuna.objects.filter(vacuna_id = self)

    datos = property(obtener_datos) # Atributo property, obtengo todas las fechas y descripciones correspondientes a cada vacuna

class DatosVacuna(models.Model):
    vacuna_id = models.ForeignKey(Vacuna, on_delete=models.CASCADE)
    descripcion = models.CharField(max_length=30)
    fecha_puesta = models.DateField()

Then later in the template you could make use of that property attribute in the following way:

{% for vacuna in vacunas %}
    <tr>
        <td>{{ vacuna.nombre }}</td>
        {% for dato in vacuna.datos %}
            <td>--{{ dato.fecha_puesta }}--{{dato.descripcion}}</td>
        {% endfor %}
    </tr>
{% endfor %}

In this way you go over all the dates and descriptions corresponding to each vaccine. In addition, since the database model is standardized, you will not have problems with the names of the vaccines, and in this way you avoid problems when changing the name of a vaccine or writing it incorrectly.

I hope I have been of help.

    
answered by 28.03.2017 в 23:16