Custom model field/form: __init__() got multiple values for keyword argument 'baz'

1,120 views
Skip to first unread message

mhulse

unread,
Sep 20, 2011, 1:38:15 PM9/20/11
to django...@googlegroups.com
Hello,

The problematic code can be found here:

<https://gist.github.com/1229708>

TypeError:

__init__() got multiple values for keyword argument 'baz'

I have spent the last couple days trying to figure out how to pass a kwarg from model to fields/forms (and eventually to a widget).

TBTH, I have had a heck of a time finding a contemporary example of how to do this that fits the needs of my project. The docs are definitely detailed, and the Django source code is helpful, but I have yet to piece this puzzle together. :(

Any tips would be greatly appreciated.

Django (1, 4, 0, 'alpha', 0)

Michal Petrucha

unread,
Sep 20, 2011, 6:36:36 PM9/20/11
to django...@googlegroups.com

Well, the problem is that your defined fields.TestField.__init__ takes
baz as either a keyword argument, or as the first positional arg. As
with related fields, to pass the verbose name in this case you have to
specify it as a keyword argument as well or specify your custom
argument first and shift all other positional arguments. So, to fix
your issue, choose either of the two following::

foo = TestField(baz='bar', verbose_name=u'Foo field',
max_length=100, help_text='Foo baz bar?')

foo = TestField('bar', u'Foo field', max_length=100,
help_text='Foo baz bar?')

Michal

signature.asc

Micky Hulse

unread,
Sep 20, 2011, 7:18:28 PM9/20/11
to django...@googlegroups.com
On Tue, Sep 20, 2011 at 3:36 PM, Michal Petrucha <michal....@ksp.sk> wrote:
> Well, the problem is that your defined fields.TestField.__init__ takes
> baz as either a keyword argument, or as the first positional arg. As
> with related fields, to pass the verbose name in this case you have to
> specify it as a keyword argument as well or specify your custom
> argument first and shift all other positional arguments. So, to fix
> your issue, choose either of the two following::

Ahhh, thank you for the clarification and for providing me with a
solution!!! I really appreciate it. :)

For some reason, I never thought to re-arrange the order of the
args/kwargs in the model TestField().

Optimally, I think I would prefer to keep the verbose name in the
first position (as an arg, not kwarg) in order to keep things
consistent.

If you look at fields.py:

<https://gist.github.com/1229708#file_fields.py>

Line #s 13 and 16, I do this:

def __init__(self, *args, **kwargs):
self._baz = kwargs.get('baz', None)

But when I do that, I get this error:

File "/.../test/fields.py", line 19, in __init__
TypeError: __init__() got an unexpected keyword argument 'baz'

Is there a way to pass around keyword arguments (from the model
TestField()) without having to be explicit?

Sorry if noob questions. I still consider myself a Python/Django noobie :)

A billion thanks for all of your help! I really appreciate it.

Have a nice day!

Cheers
Micky

Michal Petrucha

unread,
Sep 21, 2011, 9:08:56 AM9/21/11
to django...@googlegroups.com
On Tue, Sep 20, 2011 at 04:18:28PM -0700, Micky Hulse wrote:
> Optimally, I think I would prefer to keep the verbose name in the
> first position (as an arg, not kwarg) in order to keep things
> consistent.
>
> If you look at fields.py:
>
> <https://gist.github.com/1229708#file_fields.py>
>
> Line #s 13 and 16, I do this:
>
> def __init__(self, *args, **kwargs):
> self._baz = kwargs.get('baz', None)
>
> But when I do that, I get this error:
>
> File "/.../test/fields.py", line 19, in __init__
> TypeError: __init__() got an unexpected keyword argument 'baz'

In this case, the problem is a little bit different.
models.CharField.__init__ knows nothing about any 'baz' keyword
argument. With your current code, however, you keep the 'baz' item in
the kwargs dictionary and also pass this argument to
CharField.__init__. That's the cause of this error.

The remedy is simple, instead of kwargs.get use kwargs.pop.

Michal

signature.asc

Micky Hulse

unread,
Sep 21, 2011, 1:02:37 PM9/21/11
to django...@googlegroups.com
On Wed, Sep 21, 2011 at 6:08 AM, Michal Petrucha <michal....@ksp.sk> wrote:
> The remedy is simple, instead of kwargs.get use kwargs.pop.

Ahhh, I see! Thanks so much for the explanation and clarification.

Much appreciated.

Have an excellent day!

Cheers,
Micky

Reply all
Reply to author
Forward
0 new messages