我正在使用django-taggit为django-wiki构建一个自定义标签插件来保存标签。我无法使用.save_m2m保存表单,就像在django-taggit文档中所说的那样。标签可以保存在管理员中,但不能保存在前端表单中,因为我无法使用.save_m2m()保存。即使表单已保存,我也会收到以下错误。任何帮助将不胜感激。
type object 'SidebarForm' has no attribute 'save_m2m'
wiki_plugins.py
class TagPlugin(BasePlugin):
slug = settings.SLUG
sidebar = {
'headline': _('Tags'),
'icon_class': 'fa-picture-o',
'template': 'sidebar.html',
'form_class': forms.SidebarForm,
'get_form_kwargs': (lambda a: {'instance': models.Tag(article=a)})
}
models.py
class TagRevision(RevisionPluginRevision):
def can_write(self, user):
if not settings.ANONYMOUS and (not user or user.is_anonymous()):
return False
return RevisionPlugin.can_write(self, user)
def can_delete(self, user):
return self.can_write(user)
class Meta:
verbose_name = _('tag')
verbose_name_plural = _('tags')
db_table = 'wiki__plugins_tags_tag' # Matches label of upcoming 0.1 release
if settings.APP_LABEL:
app_label = settings.APP_LABEL
def __str__(self):
if self.current_revision:
return ugettext('Tag: %s') % self.current_revision.tagrevision
else:
return ugettext('Current revision not set!!')
@python_2_unicode_compatible
class TagRevision(RevisionPluginRevision):
tags = TaggableManager()
def inherit_predecessor(self, tag):
"""
Inherit certain properties from predecessor because it's very
convenient. Remember to always call this method before
setting properties :)"""
predecessor = tag.current_revision.tagrevision
self.plugin = predecessor.plugin
self.deleted = predecessor.deleted
self.locked = predecessor.locked
self.tag = predecessor.tag
if not skip_image_file:
try:
self.tags = predecessor.tags
except IOError:
self.tags = None
class Meta:
verbose_name = _('tag revision')
verbose_name_plural = _('tag revisions')
# Matches label of upcoming 0.1 release
db_table = 'wiki_plugins_tags_tagrevision'
if settings.APP_LABEL:
app_label = settings.APP_LABEL
ordering = ('-created',)
def __str__(self):
return ugettext('Tag Revsion: %d') % self.revision_number
forms.py
from wiki_plugins_tags import models
class SidebarForm(PluginSidebarFormMixin):
def __init__(self, article, request, *args, **kwargs):
self.article = article
self.request = request
super(SidebarForm, self).__init__(*args, **kwargs)
self.fields['tags'].required = True
def save(self, *args, **kwargs):
if not self.instance.id:
tagged = models.Tag()
tagged.article = self.article
kwargs['commit'] = False
revision = super(SidebarForm, self).save(*args, **kwargs)
revision.set_from_request(self.request)
tagged.add_revision(self.instance, save=True)
return revision
return super(SidebarForm, self).save(*args, **kwargs)
class Meta:
model = models.TagRevision
fields = ('tags',)
class RevisionForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.tags = kwargs.pop('tags')
self.request = kwargs.pop('request')
super(RevisionForm, self).__init__(*args, **kwargs)
self.fields['tags'].required = True
def save(self, *args, **kwargs):
if not self.instance.id:
kwargs['commit'] = False
revision = super(RevisionForm, self).save(*args, **kwargs)
revision.inherit_predecessor(self.tags, skip_image_file=True)
revision.deleted = False # Restore automatically if deleted
revision.set_from_request(self.request)
self.tags.add_revision(self.instance, save=True)
return revision
return super(RevisionForm, self).save(*args, **kwargs)
class Meta:
model = models.TagRevision
fields = ('tags',)