Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#10910 closed (invalid)

Unicode dictionary keys cause TypeError when used as arguments to filter()

Reported by: mostrovsky Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm using dictionaries to pass arguments to a filter. If the keys of the dictionary are unicode, this causes a TypeError similar to the ones seen in previous tickets (see #10645, which fixed this problem from the admin side of things)

>>> filter_args = {u'date_created':'2009-01-01'}
>>> MyClass.objects.filter(**filter_args)
>>> TypeError: filter() keywords must be strings

Of course, casting the keys by using str() fixes the problem.

This behavior can bee seen on trunk, revision 10630, with python2.5

Change History (3)

comment:1 by Ramiro Morales, 15 years ago

Version: 1.0SVN

Change Version field value to SVN as per ticket description.

comment:2 by Malcolm Tredinnick, 15 years ago

Resolution: invalid
Status: newclosed

This isn't a bug with Django. You can only use the str type for parameter names when calling Python functions. That applies for kwargs as well. It's normal Python behaviour and you should get into the habit of only using str objects as the keys in dictionaries passed as kwargs. So the solution to your problem is "don't do that".

comment:3 by Malcolm Tredinnick, 15 years ago

By the way, the reason that #10645 is a bug and this isn't is because in #10645 Django itself was creating the dictionary and needed to construct it properly. In this case, the user is constructing the dictionary themselves and can do the right thing in the first place.

Note: See TracTickets for help on using tickets.
Back to Top