On Fri, May 25, 2012 at 2:24 AM, bruno desthuilliers
<bruno.desthuilli...@gmail.com> wrote:
>
> On May 24, 11:50 pm, Larry Martell <larry.mart...@gmail.com> wrote:
>> I got this to work by adding the derived field to the drill down query
>> with queryset.extra, and then it was available with getattr. The only
>> issue I had was they wanted the derived field displayed with a name
>> (not col1-col2). In the first query I was able to easily do this. But
>> in the drill down query I had to do:
>>
>> extra(select={self.field_name : self.field_name})
>
> The QuerySet.extra() "select" params is a mapping of
> "attribute_name":"SQL clause", so you could just write:
>
>  extra(select={"Delta" : "col1 - col2"})
>
> of for somethin more generic:
>
>  extra(select={self.display_name: self.sql_clause})
>
> or if more than one extra field involved:
>
>
>  extra(select=self.extra_fields)
>
>
> where extra_fields = {"displayname1":"sql clause 1", "displayname2",
> "sql clause 2"}

Yes, I realize that.

>> I couldn't use the display name or the getattr failed.
>
> ???

When I first did:

extra(select={"Delta" : self.field_name})

Then getattr(self.field_name) failed with "object has no attribute
'col1-col2'". When I changed it to:

extra(select={self.field_name : self.field_name})

Then the getattr worked.

>> But then it was
>> displaying with 'col1-col2'. So then I had to explicitly test for it
>> and set the display name:
>>
>>             if field_name == 'col1-col2': display_name = 'Delta'
>>             else: display_name = field_name
>
>
>
> If you need display names that you don't want to use as field names
> (or if you want translatable display names etc), you can maintain a
> mapping of fieldname:display_name and then just do a dict lookup, ie:
>
>    display_name = display_names.get(field_name, field_name)
>
>
> Can't really help more here without seeing the actual code, but from
> years of experience with Python, I really doubt you have to write
> something as ugly as the above snippet ;)

Yes, I've been writing python for years too, and I realize I can put
in a dict. But it still has to be in 2 places - the code that
implements the first query that generates the chart, and the code that
does the drill down query when they click on a point. The drill down
code does not get passed the display name 'Delta' just the field as
'col1-col2'

-- 
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