答案 0 :(得分:1)
您可以使用coalesce
获取非空列值:
class TrafficForm(ModelForm):
class Meta:
model = Traffic
fields = ['sessions', 'new_users', 'reminder', 'campaigns', 'new_sales', 'sales_renewals']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['sessions'].widget.attrs.update({
'placeholder': 'Sessions',
...
})
for field in iter(self.fields):
self.fields[field].widget.attrs.update({
'class': 'form-control placeholder-no-fix'
})
def traffic_create(request, template_name='traffic_form.html'):
form = TrafficForm(request.POST or None)
if form.is_valid():
form.save()
return redirect('traffic_list')
return render(request, template_name, {'form': form})
def traffic_list(request, template_name='traffic_list.html'):
traffic = Traffic.objects.all()
data = {}
data['object_list'] = traffic
return render(request, template_name, data)
def traffic_update(request, pk, template_name='traffic_form.html'):
traffic = get_object_or_404(Traffic, pk=pk)
form = TrafficForm(request.POST or None, instance=traffic)
if form.is_valid():
form.save()
return redirect('traffic_list')
return render(request, template_name, {'form': form})
def traffic_delete(request, pk, template_name='traffic_confirm_delete.html'):
traffic = get_object_or_404(Traffic, pk=pk)
if request.method == 'POST':
traffic.delete()
return redirect('traffic_list')
return render(request, template_name, {'object': traffic})
<强> SQLFiddle 强>
答案 1 :(得分:1)