Django Taggit – 不从自定义管理表单保存标记关联
坚持到这里……
从 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() 我究竟做错了什么?提前致谢! 解决方法
事实证明,form.save_m2m()是罪魁祸首.如果我从我自己的代码中取出它,并在django.contrib.admin.options.py(第983行)中对其进行了注释,则会保存关联以及标记.
显然改变django的代码并不是一个好主意,所以我最终在我的ProductAdmin(以及add_view())中覆盖了change_view().我在调用super()之后添加了标签,因此form.save_m2m()不会覆盖我的标签关联. 这很奇怪,因为它直接针对django-taggit的文档,强调了调用form.save_m2m()的重要性:http://django-taggit.readthedocs.org/en/latest/forms.html 好吧,我不知道怎么了,我可能会继续使用taggit谷歌小组并通知他们.无论如何,感谢大卫的帮助,如果没有更少的pdb是真棒,我以前不知道它:) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |