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()
我做错了什么?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以事实证明
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 asadd_view()
). I added the tags after callingsuper()
, soform.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.htmlWell 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 :)
您使用哪种标签系统?可能您需要使用
product.tags.save()
?Which tagging system are you using? Possibly you need to use
product.tags.save()
?