[英]Django inlineformset_factory and ManyToManyField field, once again
first of all, sorry for my English!首先,对不起我的英语! I have a little problem with "inlineformset_factory" and "ManyToManyField".
我对“inlineformset_factory”和“ManyToManyField”有点问题。 Perhaps the option "inlineformset_factory" isn't the right choice.
也许选项“inlineformset_factory”不是正确的选择。 I have two classes, Prodotti and Categoria.
我有两个班级,Prodotti 和 Categoria。 In models.py are
在models.py中有
class Categoria(models.Model):
''' tabella delle Categorie dei prodotti '''
NomeCategoria = models.CharField(max_length=50,blank=True,null=True)
class Prodotti(models.Model):
''' tabella Prodotti a catalogo '''
NomeProdotto = models.CharField(max_length=50,blank=True,null=True)
CategoriaProdotto = models.ManyToManyField(Categoria, related_name='prodotti_categoria')
I need a form to modify the name of a specific Categoria, es.我需要一个表单来修改特定类别的名称,es。 Antiossidante, and eventually change the list of Prodotti that have this Categoria.
Antiossidante,并最终更改具有此类别的 Prodotti 列表。 I try a lot with "inlineformset_factory" and the use of "Prodotti.CategoriaProdotto.through" but I have problems with fields, only "id" is accepted.
我尝试了很多“inlineformset_factory”和“Prodotti.CategoriaProdotto.through”的使用,但我遇到了字段问题,只接受“id”。 ie
IE
ProdottiFormset = inlineformset_factory(Categoria, Prodotti.CategoriaProdotto.through, fields=('id',))
But, changing the name of Categoria it isn't saved.但是,更改类别的名称不会保存。 This is my project:
这是我的项目:
views.py
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, fields=('id',))
if request.method == "POST":
form = CategoriaModelForm(request.POST, request.FILES, instance=categoria)
formset = ProdottiFormset(request.POST, instance=categoria)
if formset.is_valid():
formset.save()
return render(request, "dopo_modifica_categoria.html")
# return redirect(...)
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)
Template:模板:
{% extends 'base.html'%}
{% block head_title %}{{ block.super }} - Modifica categoria{% endblock head_title %}
{% load crispy_forms_tags %}
{% block content %}
<form method="post">
{% csrf_token %}
{{ form.NomeCategoria|as_crispy_field }}
{{ formset.as_p }}
<button type="submit" class="btn btn-primary btn-sm">Modifica</button>
<br>
</form>
{% endblock content %}
And form.py和form.py
class CategoriaModelForm(forms.ModelForm):
class Meta:
model = Categoria
fields = "__all__"
Thank you very much for every suggestion!非常感谢您的每一个建议!
The next step should create a new Categoria and Prodotti in that Categoria.下一步应该在该类别中创建一个新的类别和 Prodotti。
If you are trying to save NomeCategoria then currently you are saving formset but not the form.如果您正在尝试保存 NomeCategoria,那么目前您正在保存表单集而不是表单。 Save the form and I guess this will solve your problem.
保存表单,我想这将解决您的问题。
By modifying view I was also able to save new products in that specific category:通过修改视图,我还能够在该特定类别中保存新产品:
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)
Goal!目标!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.