Django Taggit - 标签关联未从自定义管理表单中保存

发布于 2024-11-06 05:07:33 字数 1092 浏览 0 评论 0原文

这里快要疯了... 在 shell 中,我可以执行以下操作:

product.tags.add("a_new_tag")

将标签添加到数据库,并且与产品的标签关联正常工作。 (即,当我执行 Product.objects.filter(tags__name__in=["a_new_tag"] 时,相应的产品会吐出)

我需要做的是在处理表单时在管理中添加一些标签。

这是我的表单代码(阅读第 4 行和第 5 行中的注释):

class ProductForm(ModelForm):
        def save(self, commit=True):
            product = super(ProductForm, self).save(commit=False)
            product.type="New Type to Confirm Info is being Saved Correctly" //this is saved to the product.
            product.tags.add('a_new_tag_1') //the tag is saved to the taggit db, but the association with the product isn't kept.
            product.save()
            self.save_m2m()
            return m

我尝试在管理类中进行保存,但这也不起作用:

class ProductAdmin(admin.ModelAdmin):
    form = ProductForm
    def save_model(self, request, obj, form, change):
        obj.type="new_type" //this works 
        obj.tags.add("a_new_tag_2") //tag association not saved
        obj.save()
        form.save_m2m()

我做错了什么?

Going nuts over here...
From within the shell, I can do:

product.tags.add("a_new_tag")

The tag is added to the db, and the tag association with the product works correctly. (i.e., when I do Product.objects.filter(tags__name__in=["a_new_tag"] the appropriate product spits out)

What I need to do is add some tags in the admin when the form is processed.

Here is my form code (read the comments in lines 4 and 5):

class ProductForm(ModelForm):
        def save(self, commit=True):
            product = super(ProductForm, self).save(commit=False)
            product.type="New Type to Confirm Info is being Saved Correctly" //this is saved to the product.
            product.tags.add('a_new_tag_1') //the tag is saved to the taggit db, but the association with the product isn't kept.
            product.save()
            self.save_m2m()
            return m

I tried to do the saving in the admin class instead, but this doesn't work either:

class ProductAdmin(admin.ModelAdmin):
    form = ProductForm
    def save_model(self, request, obj, form, change):
        obj.type="new_type" //this works 
        obj.tags.add("a_new_tag_2") //tag association not saved
        obj.save()
        form.save_m2m()

What am I doing wrong? Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

燕归巢 2024-11-13 05:07:33

所以事实证明 form.save_m2m() 是罪魁祸首。如果我从自己的代码中取出它,并在 django.contrib.admin.options.py (第 983 行)中将其注释掉,则关联和标签都会被保存。

显然,更改 django 的代码不是一个好主意,因此我最终在 ProductAdmin 中覆盖了 change_view() (以及 add_view())。我在调用 super() 后添加了标签,因此 form.save_m2m() 不会覆盖我的标签关联。

这很奇怪,因为它直接违背了 django-taggit 的文档,该文档强调了调用 form.save_m2m() 的重要性:http://django-taggit.readthedocs.org/en/latest/forms.html

好吧,我不知道发生了什么,我可能会继续taggit google groups 并通知他们。无论如何,感谢大卫的帮助,如果 pdb 太棒了,我之前不知道:)

So it turns out that form.save_m2m() was the culprit. If I took it out of my own code, and commented it out in django.contrib.admin.options.py (line 983), the associations as well as the tag were saved.

Obviously it's not a good idea to change django's code, so I ended up overriding change_view() in my ProductAdmin (as well as add_view()). I added the tags after calling super(), so form.save_m2m() wouldn't overwrite my tag associations.

This is strange because it goes directly against django-taggit's documentation which emphasizes how important it is to call form.save_m2m() : http://django-taggit.readthedocs.org/en/latest/forms.html

Well I dunno what's up, I'll probably go on the taggit google groups and notify 'em. In any case thanks David for your help, if nothing less pdb is AWESOME and I did not know about it before :)

烟雨扶苏 2024-11-13 05:07:33

您使用哪种标签系统?可能您需要使用product.tags.save()

Which tagging system are you using? Possibly you need to use product.tags.save()?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文