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


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

???

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

HTH

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