Error when passing arguments to custom __init__ of ModelForm

304 views
Skip to first unread message

Jani Rahkola

unread,
Jun 13, 2010, 7:06:47 PM6/13/10
to Django users
Hei,

I hope someone could shed some light on this one.
I have these in their appropriate files:

class ShoppinglistForm(ModelForm):

def __init__(self, user=False, *args, **kwargs):
ModelForm.__init__(self, *args, **kwargs)
self.fields['pantry'].empty_label = None
if user:
self.fields['pantry'].queryset =
Product.objects.filter(owner=user)

class Meta:
model = Shoppinglist

def new(request):
if request.method == 'POST':
form = ShoppinglistForm(request.POST)
if form.is_valid():
try:
list = Shoppinglist(name=form.cleaned_data['name'],

pantry=form.cleaned_data['pantry'])
list.save()
return redirect('blackem.users.views.home')
except ObjectDoesNotExist:
return redirect('blackem.users.views.home')
else:
form = ShoppinglistForm(user = request.user)
return render_to_response('shoppinglists/shoppinglist_form.html',
{'form': form,
'logged': True},

context_instance=RequestContext(request))

After pressing Submit on the form, I get this:

Traceback:
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py"
in get_response
100. response = callback(request,
*callback_args, **callback_kwargs)
File "/usr/lib/python2.6/site-packages/django/contrib/auth/
decorators.py" in _wrapped_view
25. return view_func(request, *args, **kwargs)
File "/home/jani/projects/blackem/shoppinglists/views.py" in new
20. form = ShoppinglistForm(request.POST)
File "/home/jani/projects/blackem/shoppinglists/models.py" in __init__
18. self.fields['pantry'].queryset =
Pantry.objects.filter(owner=my_user)
File "/usr/lib/python2.6/site-packages/django/db/models/manager.py" in
filter
141. return self.get_query_set().filter(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in
filter
550. return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in
_filter_or_exclude
568. clone.query.add_q(Q(*args, **kwargs))
File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py"
in add_q
1131. can_reuse=used_aliases)
File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py"
in add_filter
1071. connector)
File "/usr/lib/python2.6/site-packages/django/db/models/sql/where.py"
in add
66. value = obj.prepare(lookup_type, value)
File "/usr/lib/python2.6/site-packages/django/db/models/sql/where.py"
in prepare
299. return self.field.get_prep_lookup(lookup_type,
value)
File "/usr/lib/python2.6/site-packages/django/db/models/fields/
related.py" in get_prep_lookup
134. return self._pk_trace(value, 'get_prep_lookup',
lookup_type)
File "/usr/lib/python2.6/site-packages/django/db/models/fields/
related.py" in _pk_trace
196. v = getattr(field, prep_func)(lookup_type, v, **kwargs)
File "/usr/lib/python2.6/site-packages/django/db/models/fields/
__init__.py" in get_prep_lookup
292. return self.get_prep_value(value)
File "/usr/lib/python2.6/site-packages/django/db/models/fields/
__init__.py" in get_prep_value
476. return int(value)

Exception Type: TypeError at /shoppinglists/new
Exception Value: int() argument must be a string or a number, not
'QueryDict'

Taking away the 'user' keyword argument makes it work nicely. But I
need to include only the user's pantries in the choices of the select
wiget.

Alexander Jeliuc

unread,
Jun 13, 2010, 7:13:24 PM6/13/10
to django...@googlegroups.com
Read about python version difference in function overloading domain  
------>ModelForm.__init__(self, *args, **kwargs)   # this can be different


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Karen Tracey

unread,
Jun 13, 2010, 7:25:11 PM6/13/10
to django...@googlegroups.com
On Sun, Jun 13, 2010 at 7:06 PM, Jani Rahkola <jani.r...@gmail.com> wrote:
Hei,

I hope someone could shed some light on this one.
I have these in their appropriate files:

class ShoppinglistForm(ModelForm):

   def __init__(self, user=False, *args, **kwargs):
       ModelForm.__init__(self, *args, **kwargs)
       self.fields['pantry'].empty_label = None
       if user:
           self.fields['pantry'].queryset =
Product.objects.filter(owner=user)

   class Meta:
       model = Shoppinglist

def new(request):
   if request.method == 'POST':
       form = ShoppinglistForm(request.POST)

You need to pass user=request.user here, as you do for the non-POST case. Whatever it is ending up being from request.POST or the default arguments to the __init__ function is not working.

Karen
--
http://tracey.org/kmt/

Jani Rahkola

unread,
Jun 14, 2010, 6:21:41 AM6/14/10
to Django users
Thanks!

Yep, it was a problem with the keyword argument 'user'. Solved the
issue by first calling the argument 'my_user' and not explicitly
stating it in the __init__ function signature.

def __init__(self, *args, **kwargs):
user = kwargs.pop('my_user', False)
...

On Jun 14, 2:25 am, Karen Tracey <kmtra...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages