我如何通过 Django 中选中的表单复选框过滤查询集?
how can i filter queryset by checked form checkboxes in django?
我需要根据 django 形状中标记的复选框更新模型
我怎样才能在查询
中只得到一些table字段
应该通过查询集更新“checked”行
models.py
class moIn(models.Model):
date = models.DateTimeField(auto_now_add=True, verbose_name='')
dateUpdate = models.DateTimeField(auto_now=True)
ts = models.IntegerField(verbose_name='')
pl = models.IntegerField(verbose_name='')
rem = models.IntegerField(verbose_name='')
comment = models.TextField(max_length=200, verbose_name='', blank=True)
staffer = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='')
checked = models.BooleanField(verbose_name='', default=False)
checkedUser = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='', blank=True, null=True, related_name='checkedUser')
通过单击此复选框,您将需要接收数据库记录
forms.py
class checkForm(ModelForm):
checked = fields.BooleanField(required=False)
class Meta:
model = moIn
fields = {"id", "checked"}
views.py
def dashboard(request):
if request.user.groups.filter(name='DashBoardAccess').exists():
form = checkForm
f = tableDashFilter(request.GET, queryset=moIn.objects.all())
if request.method == 'POST':
form = checkForm(request.POST)
if form.is_valid():
tt = form.save(commit=False)
data = form.cleaned_data
field = data['checked']=True
f.qs.filter(checked=field).update(checked=True, checkedUser=request.user)
return HttpResponse('ok')
else:
context = {
'filter': f,
'form': form
}
return render(request, 'dashboard/index.html', context)
else:
raise Http404()
在粗体行中,您只需要获取那些标记了复选框的行
f.qs.filter(checked=field).update(checked=True, checkedUser=request.user)
查询集使用“.values()”即可获取所有字段,与foreignKey一起使用需要显式指定模型字段:
f = tableDashFilter(request.GET, queryset=moIn.objects.values('id','date','ts','pl','rem','comment','checked','staffer__username','checkedUser__username'))
输入的“值”,也将通过以下方式获得:
由于可以有多个值(标记的复选框),因此会有一个“.getlist”
checkID = request.POST.getlist('checked')
查询设置过滤器:
f.qs.filter(id__in=checkID).update(checked=True, checkedUser=request.user)
在html模板中通过循环,遍历并插入模型id
的输入值
我需要根据 django 形状中标记的复选框更新模型 我怎样才能在查询
中只得到一些table字段应该通过查询集更新“checked”行 models.py
class moIn(models.Model):
date = models.DateTimeField(auto_now_add=True, verbose_name='')
dateUpdate = models.DateTimeField(auto_now=True)
ts = models.IntegerField(verbose_name='')
pl = models.IntegerField(verbose_name='')
rem = models.IntegerField(verbose_name='')
comment = models.TextField(max_length=200, verbose_name='', blank=True)
staffer = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='')
checked = models.BooleanField(verbose_name='', default=False)
checkedUser = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='', blank=True, null=True, related_name='checkedUser')
通过单击此复选框,您将需要接收数据库记录 forms.py
class checkForm(ModelForm):
checked = fields.BooleanField(required=False)
class Meta:
model = moIn
fields = {"id", "checked"}
views.py
def dashboard(request):
if request.user.groups.filter(name='DashBoardAccess').exists():
form = checkForm
f = tableDashFilter(request.GET, queryset=moIn.objects.all())
if request.method == 'POST':
form = checkForm(request.POST)
if form.is_valid():
tt = form.save(commit=False)
data = form.cleaned_data
field = data['checked']=True
f.qs.filter(checked=field).update(checked=True, checkedUser=request.user)
return HttpResponse('ok')
else:
context = {
'filter': f,
'form': form
}
return render(request, 'dashboard/index.html', context)
else:
raise Http404()
在粗体行中,您只需要获取那些标记了复选框的行
f.qs.filter(checked=field).update(checked=True, checkedUser=request.user)
查询集使用“.values()”即可获取所有字段,与foreignKey一起使用需要显式指定模型字段:
f = tableDashFilter(request.GET, queryset=moIn.objects.values('id','date','ts','pl','rem','comment','checked','staffer__username','checkedUser__username'))
输入的“值”,也将通过以下方式获得: 由于可以有多个值(标记的复选框),因此会有一个“.getlist”
checkID = request.POST.getlist('checked')
查询设置过滤器:
f.qs.filter(id__in=checkID).update(checked=True, checkedUser=request.user)
在html模板中通过循环,遍历并插入模型id
的输入值