On Sun, Aug 16, 2009 at 1:28 PM, rmschne <rmsc...@gmail.com> wrote:

>
> I'm extracting data from a database and then cross-tabbing the data
> (with some Python) code to create a list of dictionary data.  Because
> the key names are derived from data in the database, and the database
> is composed of unicode text, the keys are unicode, e.g. here are three
> records in the list of dictionaries:
>
> 'Date': datetime.date(2009, 3, 31), (u'ORM',): 1L, (u'ORMR',): 23L},


You might want to revisit how you are creating these dictionaries, because
your 'unicode' keys are not unicode.  They are single-element tuples where
the single elements are unicode strings.  Note the parens and comma in
(u'ORM',): 1L, for example.  If the key was the unicode string u'ORM' this
would show up as simply u'ORM': 1L.


>
> [snip]
> The template is
> {% for li in member_stats %}
> {{ li.Date }} {{li.HON }} {{li.ORM }} {{li.ORMR }} {{li.CORP1 }}
> {{li.CORP2 }} {{li.CORP3 }}
> {% endfor %}
>
> The Date fields come out as expected.  The other fields do not.
>
> Is it because they are Unicode? If so, how to deal with this.  As
> mentioned, these aren't hard-coded, but are coming originally from the
> data to construct a cross tab table (sometimes called Pivot Table).
>

>From a template li.HON will translate to the dictionary lookup li[u'HON'] in
Python.  That will find a matching unicode key u'HON' but you don't have
that, your keys are tuples.  You can experiment in the shell to see what
happens:

>>> key1 = u'ORM'
>>> key2 = (u'ORMR',)
>>> d = {key1: 1L, key2: 23L}
>>> d
{u'ORM': 1L, (u'ORMR',): 23L}
>>> d['ORM']
1L
>>> d['ORMR']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'ORMR'
>>> d[('ORMR',)]
23L
>>>

I think you want to fix your dictionary creation step to actually create
keys that are unicode, not single-element tuples.

Karen

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to