这是我的观点。
@ login_required
def product_list(request):
products = ProductBasicModels.objects.filter(whose=request.user).prefetch_related('category', 'category__category').order_by('category__ordering_num')
context = {'products': products}
return render(request, 'medicalapp_1/products_h.html', context)
@ login_required
def product_edit(request, pk):
post = get_object_or_404(ProductBasicModels, pk=pk)
if request.method == 'POST':
form = ProductForm(request.POST, instance=post)
if form.is_valid():
post = form.save(commit=False)
post.whose = request.user
post.save()
return redirect('product_list')
else:
form = ProductForm(instance=post)
return render(request, 'medicalapp_1/product_add.html', {'form': form})
和我的模特
class SubCategory(models.Model):
category = models.ForeignKey('Category', on_delete=models.CASCADE)
name = models.CharField(max_length=50)
ordering_num = models.IntegerField(default=0)
class Meta:
ordering = ['ordering_num']
def __str__(self):
return self.name
class ProductBasicModels(models.Model):
whose = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
check = models.BooleanField(default=False) # this is for Checkbox.
category = models.ForeignKey(SubCategory, on_delete=models.CASCADE) # Change this with Checkbox
name = models.CharField(max_length=50)
standard = models.CharField(max_length=50)
maker = models.CharField(max_length=50, blank=True)
outbox = models.CharField(max_length=50, blank=True)
extra = models.CharField(max_length=100, blank=True)
orderto = models.ForeignKey(OrderCompany, null=True, blank=True, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
表单
class ProductForm(forms. ModelForm):
class Meta:
model = ProductBasicModels
fields = ('check', 'category', 'name', 'standard', 'maker', 'outbox', 'extra', 'orderto')
def __init__(self, *args, **kwargs):
super(ProductForm, self).__init__(*args, **kwargs)
for field_name, field in self.fields.items():
field.widget.attrs['class'] = 'form-control'
和我的模板..
<tbody>
{% for product in subcat.list %}
<tr>
<td class="text-center">
<label class="btn btn-default btn-outline btn-block">
<input type="checkbox" name="product_selected" value="{{product.id}}" />
</label>
</td>
<td class="text-center">{{ product.name }}</td>
<td class="text-center">{{ product.standard }}</td>
<td class="text-center">{{ product.maker }}</td>
<td class="text-center">{{ product.outbox }}</td>
<td class="text-center">{{ product.extra }}</td>
<td class="text-center">{{ product.orderto }}</td>
<td class="text-center">
<a href="{% url 'product_edit' pk=product.pk %}"
class="btn btn-info btn-sm">
<i class="fa fa-edit"> EDIT</i>
</a>
</td>
<td class="text-center">
<a href="{% url 'product_del' pk=product.pk %}"
class="btn btn-danger btn-sm btn-outline">
<i class="fa fa-trash"></i>
</a>
</td>
</tr>
{% endfor %}
</tbody>
我正在做如下所示。
我需要做的是立刻更改所有Checked(图像中的红色)产品(CheckBox)的类别。 如何使用该列表进行检查? 永远感谢你的帮助。 对于没有足够学习的问题,我很抱歉。
答案 0 :(得分:0)
您可以使用product_selected复选框和必须设置的类别创建表单。在表单提交(在product_list视图中的POST处理程序代码)中,您可以通过以下方式更新所有选定的类别:
在您的模板中
<form method="POST" action="{% url 'products:list' %}">
{{ products_form.category }}
<input type="submit" name="update_products" value="Save">
...
<tbody>
{% for product in subcat.list %}
<tr>
<td class="text-center">
<label class="btn btn-default btn-outline btn-block">
<input type="checkbox" name="product_selected" value="{{product.id}}" />
</label>
...
</from>
添加新表单:
class ProductsUpdateForm(forms. Form):
product_selected = forms.MultipleChoiceField()
category = forms.ChoiceField()
def __init__(self, *args, **kwargs):
products = kwargs.pop('products', [])
super(ProductForm, self).__init__(*args, **kwargs)
# TODO: you need to set your allowed category choices
# self.fields['category'].choices = get_allowed_categories()
self.fields['product_selected'].choices = [(product.id, product.id) for product in products]
在您的视图中验证表单并更新产品:
@ login_required
def product_list(request):
products = ProductBasicModels.objects.filter(whose=request.user).prefetch_related('category', 'category__category').order_by('category__ordering_num')
products_form = ProductsUpdateForm(request.POST or None, products=products)
if 'update_products' in request.POST:
if products_form.is_valid():
data = form.cleaned_data
ProductBasicModels.objects.filter(
id__in=data['product_selected']
).update(category_id=data['category'])
context = {
'products': products,
'products_form': products_form
}
return render(request, 'medicalapp_1/products_h.html', context)
请注意,这不是一个完全正常工作的代码,而是一个如何实现它的示例。