I was wondering if someone was familiar with TreeNodeChoiceField from mptt in Django. I am trying to use this feature in a form to post an article on a blog. But when I try to create a new post it says that the object associated is not iterable. This doesn't happen with a get method or when I simply use a forms.ModelMultipleChoiceField. I was thus wondering if someone knew how I could use TreeNodeChoiceField without this issue. Thank you for any input!!
Here is the forms.py
class BlogForm(forms.ModelForm):
sdg = forms.ModelMultipleChoiceField(
queryset=SDG.objects.all(),
widget=forms.CheckboxSelectMultiple
)
value_chain = TreeNodeChoiceField(queryset=Value_chain.objects.all())
industry = forms.ModelMultipleChoiceField(
queryset=Industry.objects.all()
)
class Meta:
model = Blog
fields = ["title", "sdg", "value_chain", "industry", "contenu"]
and here is my views.py
def blog_create(request):
if request.method == "POST":
blog_form = BlogForm(request.POST)
docu_form = DocumentFormBlog(request.POST, request.FILES)
if blog_form.is_valid() and docu_form.is_valid():
blog_form.instance.user = request.user
blog = blog_form.save()
docu_form.instance.blog_related = blog
docu_form.save()
messages.success(request, 'Your blog was successfully created!')
return redirect('knowledge:search_blog')
else:
messages.error(request, 'Please correct the error below.')
else:
blog_form = BlogForm(request.POST)
docu_form = DocumentFormBlog(request.POST, request.FILES)
return render(request, "dist/inside/knowledge/blog/create_blog.html", context={"blog_form": blog_form, "docu_form": docu_form})
In the end, to make it work, I had to create a new form, only for the TreeNodeChoiceField that would call the appropriate Model:
"""
"""
This is not the best answer, so if anyone finds something better, please let me know!