Opened 11 years ago

Closed 11 years ago

Last modified 11 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: master
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


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 Changed 11 years ago by Ramiro Morales

Version: 1.0SVN

Change Version field value to SVN as per ticket description.

comment:2 Changed 11 years ago by Malcolm Tredinnick

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 Changed 11 years ago by Malcolm Tredinnick

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