擅长:python、mysql、java
<p>通过修改视图,我还能够保存该特定类别中的新产品:</p>
<pre><code>def ModificaCategoria(request, pk):
# recuperiamo la categoria da modificare, bisogna passare l'ID
categoria = get_object_or_404(Categoria, pk=pk)
ProdottiFormset = inlineformset_factory(Categoria, Prodotti.CategoriaProdotto.through, exclude=['id',])
if request.method == "POST":
form = CategoriaModelForm(request.POST, request.FILES, instance=categoria)
formset = ProdottiFormset(request.POST, instance=categoria)
if formset.is_valid() and form.is_valid():
formset.save()
form.save()
return render(request, "dopo_modifica_categoria.html")
else:
categoria = Categoria.objects.get(pk=pk)
form = CategoriaModelForm(instance=categoria)
formset = ProdottiFormset(instance=categoria)
context = {
"form": form,
"formset": formset,
}
return render(request, "modifica_categoria.html", context)
</code></pre>
<p>进球</p>