Code

Opened 6 years ago

Closed 6 years ago

#6728 closed (worksforme)

Dictionary with django object as key failing.

Reported by: dl@… Owned by: nobody
Component: Core (Other) Version: 0.96
Severity: Keywords: object dictionary key model lookup
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 a dictionary to store a mapping between django objects and wxPython controls.

>>> print controls
controls = {
<MyObject: P001>: <wxTextCtrl instance at _08d89808_p_wxTextCtrl>,
<MyObject: P010>: <wxTextCtrl instance at _28449708_p_wxTextCtrl>,
<MyObject: P007>: <wxTextCtrl instance at _805f9808_p_wxTextCtrl>,
<MyObject: P006>: <wxTextCtrl instance at _78699b08_p_wxTextCtrl>,
<MyObject: P009>: <wxTextCtrl instance at _d8099a08_p_wxTextCtrl>,
<MyObject: P008>: <wxTextCtrl instance at _08ac9908_p_wxTextCtrl>,
<MyObject: P011>: <wxTextCtrl instance at _4834b008_p_wxTextCtrl>
}

I need to find the appropriate control fo a given value, but using the MyObject as key is behaving strangely:

>>> print value
<MyObject: P001>
>>> print controls[value]
KeyError: <MyObject: P001>
>>> print value in controls
False
>>> print value in controls.keys()
True
>>> for key, control in controls.items():
...     print "%r %r %r" % ( key == value, key, value)
True <MyObject: P001> <MyObject: P001>
False <MyObject: P001> <MyObject: P010>
False <MyObject: P001> <MyObject: P007>
False <MyObject: P001> <MyObject: P006>
False <MyObject: P001> <MyObject: P009>
False <MyObject: P001> <MyObject: P008>
False <MyObject: P001> <MyObject: P011>

Attachments (0)

Change History (1)

comment:1 Changed 6 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

If you're using Django 0.96, as indicated by the version you've selected for this ticket, this is the expected behavior; in Django 0.96, the base model implementation your model inherit from does not define the necessary methods to act as a dictionary key. In trunk this has changed, but if you need this functionality on 0.96, you can implement it explicitly on your model class.

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.