寻找关键字Field.save_form_data的格式

时间:2019-06-18 22:15:50

标签: django mezzanine

我有一个Mezzanine项目,正在尝试更新博客条目上的关键字。我很难获得正确的格式来调用KeywordField.save_form_data,这会调用一个将更新博客文章中的关键字的js。见下文:

来自Messanine / generic / fields.py

class KeywordsField(BaseGenericRelation):
    """
Stores the keywords as a single string into the
``KEYWORDS_FIELD_NAME_string`` field for convenient access when
searching.
"""

    default_related_model = "generic.AssignedKeyword"
    fields = {"%s_string": CharField(editable=False, blank=True,
                                 max_length=500)}

    def __init__(self, *args, **kwargs):
    """
    Mark the field as editable so that it can be specified in
    admin class fieldsets and pass validation, and also so that
    it shows up in the admin form.
    """
        super(KeywordsField, self).__init__(*args, **kwargs)
        self.editable = True

        def formfield(self, **kwargs):
    """
    Provide the custom form widget for the admin, since there
    isn't a form field mapped to ``GenericRelation`` model fields.
    """
        from mezzanine.generic.forms import KeywordsWidget
        kwargs["widget"] = KeywordsWidget
        return super(KeywordsField, self).formfield(**kwargs)


        def save_form_data(self, instance, data):
    """
    The ``KeywordsWidget`` field will return data as a string of
    comma separated IDs for the ``Keyword`` model - convert these
    into actual ``AssignedKeyword`` instances. Also delete
    ``Keyword`` instances if their last related ``AssignedKeyword``
    instance is being removed.
    """
        from mezzanine.generic.models import Keyword
        related_manager = getattr(instance, self.name)
        # Get a list of Keyword IDs being removed.
        old_ids = [str(a.keyword_id) for a in related_manager.all()]
        new_ids = data.split(",")
        removed_ids = set(old_ids) - set(new_ids)
        # Remove current AssignedKeyword instances.
        related_manager.all().delete()
        # Convert the data into AssignedKeyword instances.
        if data:
            data = [related_manager.create(keyword_id=i) for i in new_ids]
        # Remove keywords that are no longer assigned to anything.
        Keyword.objects.delete_unused(removed_ids)
        super(KeywordsField, self).save_form_data(instance, data)

从我的Views.py

class PubForm(forms.ModelForm):

   class Meta:
        model = BlogPost
        fields = ['keywords']


def UpdatePub(request, slug): 
    blog_post = BlogPost.objects.get(id=slug)
    if request.method == 'POST': 
        form = PubForm(request.POST)   
        if form.is_valid(): 
            publish_date = datetime.datetime.now()
            blog_post.status = CONTENT_STATUS_PUBLISHED             
            publish_date=publish_date
            tags=form.cleaned_data['keywords']
            blog_post.save() 
            KeywordsField.save_form_data(user,blog_post,tags)       
            return HttpResponseRedirect('/write/') 
    else:
        form = PubForm(instance=blog_post)
    return render(request, 'blog_my_pub.html', {'form' : form}) 

它抱怨字段“用户”没有属性“名称”。我为此参数尝试了许多不同的值,但无法弄清楚。任何帮助,将不胜感激。

感谢您的任何输入。

0 个答案:

没有答案