AttributeError:myview在Django中的自定义mixin中没有属性对象

时间:2014-05-21 05:09:56

标签: django mixins django-class-based-views django-generic-views

我正在尝试编写mixin以便能够部分保存表单并稍后恢复。 当表单很长并且用户无法一次性完成时,这非常有用。以下mixin代码直接来自Marty Alchin撰写的prodjango书。我在代码中注释了错误来自POST method mixin.下面的详细错误说明。

从回溯中,我认为错误来自这两个来电self.get_form(form_class)get_form_kwargs。但我不知道如何解决这个问题。

以下是 view

class ArticleCreateView(PendFormMixin, CreateView):
      form_class = ArticleForm
      model = Article
      template_name = "article_create.html"
      success_url = '/admin'

以下是 mixin

from django.views.generic.edit import FormView
from pend_form.models import PendedForm, PendedValue
from hashlib import md5



class PendFormMixin(object):
    form_hash_name = 'form_hash'
    pend_button_name = 'pend'
    def get_form_kwargs(self):
        """
        Returns a dictionary of arguments to pass into the form instantiation.
        If resuming a pended form, this will retrieve data from the database.
        """
        form_hash = self.kwargs.get(self.form_hash_name)
        print "form_hash", form_hash
        if form_hash:
            import_path = self.get_import_path(self.get_form_class())
            return {'data': self.get_pended_data(import_path, form_hash)}
        else:
            print "called"
            # print super(PendFormMixin, self).get_form_kwargs()
            return super(PendFormMixin, self).get_form_kwargs()

    def post(self, request, *args, **kwargs):
        """
        Handles POST requests with form data. If the form was pended, it doesn't follow
        the normal flow, but saves the values for later instead.
        """
        if self.pend_button_name in self.request.POST:
            print "here"
            form_class = self.get_form_class()
            print form_class
            form = self.get_form(form_class)
             #the error happens here. below print is not executed
            # print "form is ", form
            self.form_pended(form)
        else:
            super(PendFormMixin, self).post(request, *args, **kwargs)

# Custom methods follow
    def get_import_path(self, form_class):
        return '{0}.{1}'.format(form_class.__module__, form_class.__name__)
    def get_form_hash(self, form):
        content = ','.join('{0}:{1}'.format(n, form.data[n]) for n in form.fields.keys())
        return md5(content).hexdigest()
    def form_pended(self, form):
        import_path = self.get_import_path(self.get_form_class())
        form_hash = self.get_form_hash(form)
        print "in form_pended"
        pended_form = PendedForm.objects.get_or_create(form_class=import_path,
                                                       hash=form_hash)
        for name in form.fields.keys():
            pended_form.data.get_or_create(name=name, value=form.data[name])
        return form_hash
    def get_pended_data(self, import_path, form_hash):
        data = PendedValue.objects.filter(import_path=import_path, form_hash=form_hash)
        return dict((d.name, d.value) for d in data)

错误:

'ArticleCreateView' object has no attribute 'object'
Exception Location:     /Users/django/django/lib/python2.7/site-packages/django/views/generic/edit.py in get_form_kwargs, line 125


/Users/pend_form/forms.py in post

                form = self.get_form(form_class)

/Users/django/django/lib/python2.7/site-packages/django/views/generic/edit.py in get_form_kwargs

            kwargs.update({'instance': self.object})

2 个答案:

答案 0 :(得分:8)

如果你看一下django的CreateView或其父BaseCreateView的定义,你会发现它所做的就是在调用定义实际的超类方法之前指定self.object = None形式行为。那是因为它是CreateView - 没有可能存在的编辑对象。

由于您的mixin会覆盖此行为,因此当其预期self.object作为None存在时,其余机器将失败。

self.object = None添加到def post方法的第一行。

答案 1 :(得分:2)

post中分配了self.object,因此如果您覆盖post, 在致电self.object

之前,不要指望super(...).post(...)被分配