Select specific data in Django query and map the result (between th and td) in a table -


i've been avoiding way many times , think it's time ask help.

first of all, have following structure of relevant parts of project:

# models.py  class locationsprovisioning(models.model):     user = models.charfield(max_length=200)     dates = models.charfield(max_length=15)     locations = models.charfield(max_length=200)     goods = models.charfield(max_length=200)     quantities = models.charfield(max_length=200)     samples = models.charfield(max_length=200) 
# forms.py  dateinput = partial(forms.dateinput, {     'class': 'datepicker form-control',     'name': 'dates'})  class reportform(forms.form):     start_date = forms.datefield(label="start date", required=true,                                  widget=dateinput(),                                  initial=datetime.date.today() - datetime.timedelta(days=1))     end_date = forms.datefield(label="end date", required=true,                                widget=dateinput(),                                initial=datetime.date.today())     locations = forms.modelmultiplechoicefield(label='select locations',                                                queryset=locationsmodel.objects.all().order_by('name'), required=true,                                                widget=forms.selectmultiple(attrs={                                                    'class': 'form-control',                                                    'name': 'locations'                                                })) 
# views.py  def reports_view(request):     form = reportform(request.post or none)     selected_locations = ''      all_goods = goodsmodel.objects.all().order_by('name')      if request.method == "post":         if form.is_valid():             start_date = str(form.cleaned_data.get('start_date'))             end_date = str(form.cleaned_data.get('end_date'))             selected_locations = form.cleaned_data.get('locations')          else:             form = reportform()      return render(request, 'admin/coffee_app/raport.html', {         'form': form,         'selected_locations': selected_locations,         'all_goods': all_goods  # headers of table     }) 

so far good, have in template table contains headers, goods, , first column selected_locations.

the html looks (i've removed html tags readability):

<form class="form-horizontal" method="post">     {{ form.start_date.label_tag }}     {{ form.end_date.label_tag }}     {{ form.locations }}     <button type="submit" class="btn my-btn" /> </form>  <table id="example" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">     <thead>     <tr>         <th>locations</th>         {% in all_goods %}             <th>{{ }}</th>         {% endfor %}         <th>samples</th>     </tr>     </thead>     <tbody>     {% location in selected_locations %}         <tr>             <td>{{ location }}</td>             <td>{{ here_i_should_have_quantities (some kind of map between selected location , `good` }}</td>                <td>{{ here_i_should_have_probes_for_selected_locations }}</td>         </tr>     {% endfor %}     </tbody> </table> 

now, i'd kind of select (locationsprovisioning.objects.filter()) should like:

select quantities, samples  locationsprovisioning  locations in selected_locations , dates between start_date , end_date 

i know can like:

locationsprovisioning.objects.filter(dates__range=[start_date, end_date]) 

but can't seem find way do: select quantities, samples locationsprovisioning locations in selected_locations

what looks impossible me, match quantities / probes needed:

            good_1 good_2 good_3 good_4 samples locality_1    23            2      7       3 locality_2           3      40     7       5      locality_3     1     2                     3 

more details:

  • each selected_locality has sample
  • a selected_locality might not have value goods (see above table)

ps: couldn't find better title this, feel free edit it

should dates charfield or datefield? try queryset data.

locationsprovisioning.objects     .filter(locations__in=selected_locations, dates__range=(start_date, end_date))     .values('quantities','samples') 

Comments