django - django-taggit form

前端 未结 3 898
南方客
南方客 2020-12-29 12:40

I would like to use django-taggit (click here ). The documentation ( click here) talks about using ModelForm to generate the form but I have alrea

3条回答
  •  暖寄归人
    2020-12-29 13:34

    See instructions here: https://github.com/alex/django-taggit/blob/master/docs/forms.txt

    If, when saving a form, you use the commit=False option you'll need to call save_m2m() on the form after you save the object, just as you would for a form with normal many to many fields on it::

    if request.method == "POST":
        form = MyFormClass(request.POST)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.user = request.user
            obj.save()
            # Without this next line the tags won't be saved.
            form.save_m2m()
    

提交回复
热议问题