Code

Opened 5 years ago

Closed 5 years ago

Last modified 5 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: UI/UX:

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

Attachments (0)

Change History (3)

comment:1 Changed 5 years ago by ramiro

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Version changed from 1.0 to SVN

Change Version field value to SVN as per ticket description.

comment:2 Changed 5 years ago by mtredinnick

  • Resolution set to invalid
  • Status changed from new to closed

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 5 years ago by mtredinnick

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.