AttributeError: myview has no attribute object in custom mixin in Django

≡放荡痞女 提交于 2019-12-08 08:10:18

问题


I am trying to write a mixin for able to save a form partially and resume later. This is useful when the form is long and user cannot finish in one-sitting. The mixin code below comes directly from prodjango book by Marty Alchin. I have commented in the code where the error comes which is the POST method in mixin. Detailed error description below.

From the traceback, I think the error comes from these two calls self.get_form(form_class) and get_form_kwargs. but I have no idea how to fix this.

Here is the view:

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

Here is the 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)

Error:

'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})

回答1:


If you look at the definition of django's CreateView, or its parent BaseCreateView, you'll see that all it does is assigns self.object = None before calling super class methods which define the actual form behavior. That's because it's a CreateView - no object to edit could possibly exist.

Since your mixin overrides this behavior, the rest of the machinery fails when it expects self.object to exist as None.

Add self.object = None to the first line of your def post method.




回答2:


self.object is assigned in post, so if you override post, don't expect self.object ot be assigned, before you call super(...).post(...)



来源:https://stackoverflow.com/questions/23774412/attributeerror-myview-has-no-attribute-object-in-custom-mixin-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!