Issue #20 resolved

Form issue with Django 1.7b4

Patrick Bregman
created an issue

First of all, thanks for a great library!

While using this with Django 1.7b4, I noticed that every time I tried to render it (or do anything else that accesses the fieldsets property), it would throw an AttributeError. I looked around the code, and apparently it tries to access self.form.fields which doesn't exist with BetterForm. I found it at form.Meta.fields, but this is used at quite a few places, and just substituting self.form.fields with self.form.Meta.fields caused a few other issues. This leads me to believe that they aren't the same.

A workaround for seems to be to make the Form inherit from both BetterForm and Django's Form, like this:

import django.forms
import form_utils.forms

class MyForm(form_utils.forms.BetterForm, django.forms.Form):
    char = django.forms.CharField()

    class Meta:
        fieldsets = [(None, {'fields': ['char']})]

Traceback:

AttributeError at /blog/
'PostSearchForm' object has no attribute 'fields'

Traceback:
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/tank/Documents/patrickbregman.eu/src/blog/views.py" in index
  21.     return render(request, 'blog/index.html', {'posts': posts, 'form': form})
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/shortcuts.py" in render
  48.     return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/loader.py" in render_to_string
  178.         return t.render(context_instance)
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/base.py" in render
  148.             return self._render(context)
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/loader_tags.py" in render
  126.         return compiled_parent._render(context)
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/loader_tags.py" in render
  65.                 result = block.nodelist.render(context)
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/base.py" in render
  1231.                     return self.nodelist.render(new_context)
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/django/template/defaulttags.py" in render
  172.             for i, item in enumerate(values):
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/form_utils/forms.py" in __iter__
  58.             self._gather_fieldsets()
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/form_utils/forms.py" in _gather_fieldsets
  76.                 field_names = [n for n in options['fields']
File "/tank/Documents/patrickbregman.eu/lib/python3.3/site-packages/form_utils/forms.py" in <listcomp>
  77.                                if n in self.form.fields]

Exception Type: AttributeError at /blog/
Exception Value: 'PostSearchForm' object has no attribute 'fields'

Comments (4)

  1. Carl Meyer repo owner

    Hi Patrick; thanks for the report!

    I updated tox.ini in form-utils to run the tests against 1.7 so I could try to track this down. I've gotten as far as tracking it down to this commit in Django: https://github.com/django/django/commit/a2340ac6d6b7e31c7e97e8fdaf3e1d73e43b24ba

    This indicates that it might be a bug in the new implementation of six.with_metaclass - or it might that form-utils is misusing six.with_metaclass in some way. Unfortunately I no longer actively use form-utils, so this is as far as I'll be able to track this for now. If you are able (or anyone is able) to track it down and submit a patch to fix it, I'd be happy to merge said patch.

  2. Log in to comment