Pretty sure you have an indentation error there. Looking at the traceback, the line with super
is being executed when forms.py is being imported, which shouldn't usually happen if it's inside the __init__
method. So I would guess you have that line at the same level as the method definitions, and it is being executed when the form class is defined. Check you aren't mixing tabs and spaces anywhere, for example.
NameError when overriding __init__ of ModelForm
Domanda
I hope you can help me to figure out what's wrong with this code:
from django.forms import ModelForm
from models import Persona
class PersonaForm(ModelForm):
class Meta:
model = Persona
exclude = ('cuenta',)
def __init__(self, *args, **kwargs):
cuenta = kwargs.pop('cuenta')
super(PersonaForm, self).__init__(*args, **kwargs)
self.fields['organizacion'].queryset = Organizacion.objects.filter(cuenta=cuenta)
I get the following error: name 'PersonaForm' is not defined
This is the whole traceback:
Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
103. resolver_match = resolver.resolve(request.path_info)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
319. for pattern in self.url_patterns:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
347. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
342. self._urlconf_module = import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/Applications/djangostack-1.4.5-0/apps/django/django_projects/tomate/tomate/urls.py" in <module>
18. url(r'^dashboard/', include('dashboard.urls')),
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/conf/urls/__init__.py" in include
25. urlconf_module = import_module(urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/Applications/djangostack-1.4.5-0/apps/django/django_projects/tomate/dashboard/urls.py" in <module>
3. from dashboard import views
File "/Applications/djangostack-1.4.5-0/apps/django/django_projects/tomate/dashboard/views.py" in <module>
7. from forms import PersonaForm
File "/Applications/djangostack-1.4.5-0/apps/django/django_projects/tomate/dashboard/forms.py" in <module>
4. class PersonaForm(ModelForm):
File "/Applications/djangostack-1.4.5-0/apps/django/django_projects/tomate/dashboard/forms.py" in PersonaForm
12. super(PersonaForm, self).__init__(*args, **kwargs)
Exception Type: NameError at /
Exception Value: name 'PersonaForm' is not defined
I have looked for an answer around but has not found anything yet... Thanks!
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow