rue*_*ute 5 forms django admin django-taggit
在这里发疯......从外壳内,我可以这样做:
product.tags.add("a_new_tag")
Run Code Online (Sandbox Code Playgroud)
标签被添加到数据库中,标签与产品的关联工作正常。(即当我做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
Run Code Online (Sandbox Code Playgroud)
我尝试在管理类中进行保存,但这也不起作用:
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()
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?提前致谢!
事实证明,form.save_m2m()罪魁祸首就是它。如果我从自己的代码中取出它,并在 django.contrib.admin.options.py (第 983 行)中将其注释掉,则关联和标签都会被保存。
显然,更改 django 的代码不是一个好主意,因此我最终change_view()在我的 ProductAdmin(以及add_view())中进行了覆盖。我在调用后添加了标签super(),因此form.save_m2m()不会覆盖我的标签关联。
这很奇怪,因为它直接违背了 django-taggit 的文档,该文档强调了调用的重要性form.save_m2m():http://django-taggit.readthedocs.org/en/latest/forms.html
好吧,我不知道发生了什么,我可能会去 taggit 谷歌群组并通知他们。无论如何,感谢大卫的帮助,如果 pdb 太棒了,我之前不知道:)
| 归档时间: |
|
| 查看次数: |
1487 次 |
| 最近记录: |