I would like to get your help in order to display objects choosen by user 
and get some querysets according to each object.

I'm working with django 1.11.16 on this project.

*Context :*

User has to choice some things :

 - Start date 
 - End date
 - One or several publication(s)

Then, it returns a table with some querysets applied to this/these 
publication(s) and display one publication per row with associated data.

*Forms.py file :*

   
 class PublicationStatForm(forms.Form):
        publication_list = forms.ModelMultipleChoiceField(queryset=
Publication.objects.all().order_by('pub_id'))
    
        # This part doesn't work while it should be
    
        # publication_list = forms.ModelMultipleChoiceField(
        #     queryset=Publication.objects.all().order_by('pub_id'),
        #     label=_('Publication Choice'),
        #     widget=ModelSelect2MultipleWidget(
        #         model=Publication,
        #         queryset=Publication.objects.all().order_by('pub_id'),
        #         search_fields=['pub_id__icontains', 'title__icontains'],
        #     )
        # )
    
        def __init__(self, *args, **kwargs):
            super(PublicationStatForm, self).__init__(*args, **kwargs)


In this part, I have a ModelMultipleChoiceField because user can select one 
or several publication(s). The commented part doesn't work while it should 
do the job.

*Views.py file :*

In this part, I create some querysets in order to get informations about 
selected publication(s) over a selected time period. I will show you only 
2-3 querysets but it counts 6 querysets.

I pick up the list of publications, get the id and I make a loop over each 
id in the list.

   
 class StatsView(TemplateView):
        """ Create statistics pageview """
        template_name = 'freepub/stats.html'
        form_class = PublicationStatForm
    
        def get_context_data(self, **kwargs):
            subtitle = _("Statistics")
            context_data = super(StatsView, self).get_context_data(**kwargs)
            context_data['form'] = self.form_class()
    
            if "start_date" and "end_date" in self.request.GET:
                start_date = self.request.GET.get('start_date')
                end_date = self.request.GET.get('end_date')
                
    
                if start_date < end_date and "SearchPublicationPeriod" in 
self.request.GET:
                    publication_list = self.request.GET.getlist(
'publication_list')
    
                    publication_selected = Publication.objects.filter(id__in
=publication_list)
                        
                    download_all_period = Download.objects.values('usage', 
'doc__publication__pub_id').filter(
                            doc__publication__id__in=publication_list).
filter(
                            Q(creation_date__gte=start_date) & Q(
creation_date__lte=end_date)).aggregate(
                            nusage=Sum('usage'))
    
                    ... # create others querysets


                    publication_overall = zip(publication_selected, 
download_all_period, ...)


                    context_data['start_date'] = start_date
                    context_data['end_date'] = end_date
                    context_data['publication_list'] = publication_list
                    context_data['publication_selected'] = 
publication_selected
                    context_data['download_all_period'] = 
download_all_period
                    ...


            return context_data


*HTML Template file :*

In this file, I display the form and the result in a table with each row 
according to each publication.

   
 <div class="row">
      <form>
        <fieldset>
          <legend class="title"><span class="name">{% trans 'Publication 
statistics during period' %}</span></legend>
          <br>
          <form class="date-form" method="GET">
            <div class="row">
              <div class="col-md-2">
                <div class="form-group">
                  <div class='input-group date' id='datetimepicker1'>
                    <input id="start_date" autocomplete="false" type='text' 
class="form-control" placeholder="From"
                           name="start_date" required/>
                    <span class="input-group-addon">
                      <span class="glyphicon glyphicon-calendar"></span>
                    </span>
                  </div>
                </div>
              </div>
              <div class="col-md-2">
                <div class="form-group">
                  <div class='input-group date' id='datetimepicker2'>
                    <input id="end_date" autocomplete="false" type='text' 
class="form-control" placeholder="To"
                           name="end_date" required/>
                    <span class="input-group-addon">
                          <span class="glyphicon glyphicon-calendar"></span>
                      </span>
                  </div>
                </div>
              </div>
              <div class="col-md-7">
                {{ form.publication_list|as_crispy_field }}
              </div>
            </div>
            <input id="submit-date-stats" type="submit" class="btn 
btn-default" name="SearchPublicationPeriod"
                   value="{% trans 'Submit' %}"/><br/>
          </form>
    
          <table id="period-table" class="table table-bordered 
table-striped table-condensed table_model">
            <caption>
              <span><strong>Request over period : {{ 
start_date|date:"Y/m/d" }} to {{ end_date|date:"Y/m/d" }}</strong></span>
            </caption>
            <tbody>
            <tr>
              <th>{% trans 'Period' %}</th>
              <th>{% trans 'Publication' %}</th>
              <th>{% trans 'Downloads/Requests' %}</th>
              <th>{% trans 'Best country' %}</th>
              <th>{% trans 'Best format' %}</th>
              <th>{% trans 'Best customer' %}</th>
            </tr>
    
            {% for publication_selected, download_all_period, 
request_all_period, best_country_period, best_format_period, 
best_customer_period in publication_overall %}
              {% if download_all_period %}
                <tr>
                  <td>{{ start_date|date:"Y/m/d" }} to {{ 
end_date|date:"Y/m/d" }}</td>
                  <td>{{ publication_selected }}</td>
                  <td><span class="badge alert-danger">{{ 
download_all_period.nusage }}</span> / <span
                    class="badge alert-info">{{ request_all_period.count }}
</span></td>
                  <td>{{ best_country_period.name_fr }}</td>
                  {% if best_format_period.doc__format == "pdf" %}
                    <td><span class="badge alert-danger">{{ 
best_format_period.doc__format }}</span></td>
                  {% else %}
                    <td><span class="badge alert-info">{{ 
best_format_period.doc__format }}</span></td>
                  {% endif %}
                  <td>{{ best_customer_period.email }}</td>
                </tr>
    
              {% else %}
                <tr>
                  <td>{{ start_date|date:"d/m/Y" }} to {{ 
end_date|date:"d/m/Y" }}</td>
                  <td>{{ publication_selected }}</td>
                  <td>{% trans 'No downloads/No requests' %}</td>
                  <td>{% trans 'No country' %}</td>
                  <td>{% trans 'No format' %}</td>
                  <td>{% trans 'No customer' %}</td>
                </tr>
              {% endif %}
            {% endfor %}
    
            </tbody>
          </table>
        </fieldset>
      </form>
    </div>


*Example :*

If I select 3 publications for example, this is what I get :

[image: search.png]


The first thing is the ModelMultipleChoiceField which is not good, because 
I would like to get search bar and select publication other than 
highlighting publications.

The second issue is the result displayed in the table. He is empty and 
don't create the number of row according to number of publication(s) 
selected.

Thank you if you can help me !

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/65e45fb6-9c5f-49b2-86ea-a867a54a43de%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to