ここに行きます...シェル内から、私はできる:
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()
私は何を間違っていますか?前もって感謝します!