我一直避免这种方式太多次,我认为现在是时候寻求帮助了。
首先,我的项目相关部分的结构如下:
# 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 some 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 # those will be the headers of the table
})
到目前为止,我的模板中有一个表格,其中包含标题goods
,第一列包含selected_locations
。
html看起来像这样(我已删除了一些html标签以提高可读性):
<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>
{% for good in all_goods %}
<th>{{ good }}</th>
{% endfor %}
<th>Samples</th>
</tr>
</thead>
<tbody>
{% for location in selected_locations %}
<tr>
<td>{{ location }}</td>
<td>{{ here_I_should_have_quantities (some kind of map between the selected location and the `good` }}</td>
<td>{{ here_I_should_have_probes_for_selected_locations }}</td>
</tr>
{% endfor %}
</tbody>
</table>
现在,我想做某种选择(LocationsProvisioning.objects.filter()
)应该是这样的:
SELECT quantities, samples
FROM LocationsProvisioning
WHERE locations in selected_locations and dates BETWEEN start_date AND end_date
我知道我可以这样做:
LocationsProvisioning.objects.filter(dates__range=[start_date, end_date])
但我似乎无法找到办法:SELECT quantities, samples FROM LocationsProvisioning WHERE locations in selected_locations
对我来说看起来不可能的事情也只在需要时与数量/探针匹配:
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
更多详情:
selected_locality
都有sample
selected_locality
可能没有所有goods
的值(请参见上表)PS:我找不到更好的标题,所以随时编辑
答案 0 :(得分:1)
日期应该是CharField还是DateField?尝试使用此查询集来获取数据。
LocationsProvisioning.objects
.filter(locations__in=selected_locations, dates__range=(start_date, end_date))
.values('quantities','samples')