Thursday, October 28, 2010

Re: adding field to query set

Ok the motive is for this reason.
I have a table generator class that display an html table from the
query set depending on the columns that you selected
class TableGenerator(name,mapping,data):

def __init__(self,name,mapping,data):
self._mapping = mapping
self._name = name
self._data = data
def render_table(self):
# pseudo code
* iterate through the mapping construct the table header
* iterate through self._data queryset and construct the columns
for the table body based on the mapping

-- mapping is a variable that holds the mapping of table header with
the actual field name of the model attribute
-- data is just the queryset object.

so here I want to generate a table to display every attribute in
project while also display the related fields of user.first_name and
user.last_name concatenated.
How can i refer to that field in my TableGenerator class using the
mapping generically?


On Oct 28, 3:27 pm, Jumpfroggy <rocketmonk...@gmail.com> wrote:
> What's your motive?  Are you worried about performance?
>
> If performance is not an issue, you can just do this:
>
>     for project in Project.objects.all():
>         print project.user.first_name + ' ' + project.user.last_namea
>
> This might speed things up:
>
>     projects = Project.objects.all().select_related('user')
>     for project in projects:
>         print project.user.first_name + ' ' + project.user.last_namea
>
> If that's not fast enough, you may want to do something using .extra()
> or .raw().  I'm not sure how to do this effeciently with .extra().

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment