Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Error when passing arguments to custom __init__ of ModelForm [RSS Feed]

#1 June 14, 2010 00:08:09

Jani R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Error when passing arguments to custom __init__ of ModelForm


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.empty_label = None
if user:
self.fields.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,

pantry=form.cleaned_data)
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.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.

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

Offline

#2 June 14, 2010 00:13:32

Alexander J.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Error when passing arguments to custom __init__ of ModelForm


Read about python version difference in function overloading domain
------>ModelForm.__init__(self, *args, **kwargs) # this can be different

On Mon, Jun 14, 2010 at 2:06 AM, Jani Rahkola <jani.rahk...@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.empty_label = None
> if user:
> self.fields.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,
>
> pantry=form.cleaned_data)
> 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.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.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>
>

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

Offline

#3 June 14, 2010 00:25:21

Karen T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Error when passing arguments to custom __init__ of ModelForm


On Sun, Jun 13, 2010 at 7:06 PM, Jani Rahkola <jani.rahk...@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.empty_label = None
> if user:
> self.fields.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/--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 June 14, 2010 11:21:49

Jani R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Error when passing arguments to custom __init__ of ModelForm


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:
> On Sun, Jun 13, 2010 at 7:06 PM, Jani Rahkola <jani.rahk...@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.empty_label = None
> >        if user:
> >            self.fields.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/--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Error when passing arguments to custom __init__ of ModelForm [RSS Feed]

Board footer

Moderator control

Enjoy the 20th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support