Re: adding field to query set

2010-10-29 Thread Jumpfroggy
If it were my project, I'd probably add this kind of custom field to
your mapping:

mapping = {
# list the normal columns first... then:
'full_name': ['user__first_name', 'user__last_name'],
}

Then your render code would be something like this:

def render(self):
for row in self._data:
for name, value in self._mapping.items():
print 'name: %s' % name
if hasattr(value, '__iter__'):
final_value = []
for value2 in value:
final_value.append(getattr(row, value2))
final_value = ' '.join(final_value)
else:
final_value = getattr(row, value)
print 'final_value: %s' % final_value

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Re: adding field to query set

2010-10-28 Thread owidjaya
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  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-us...@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.



Re: adding field to query set

2010-10-28 Thread Jumpfroggy
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-us...@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.



adding field to query set

2010-10-28 Thread owidjaya
If I have such models

class Project(models.Model):
  project = models.CharField(max_length=200)
  user = models.ForeignKey(User) # user is from
django.contrib.auth.models => user

I would like to be able to display an an attribute that is made up of
first_name and last_name concatenated when i iterate the queryset

So in essence, can i create such sql from django without using cursor
class?

SELECT   P.*, CONCAT(A.first_name, A.last_name)
FROM project P
LEFT OUTER JOIN auth_user A ON A.id = P.user_id

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



adding field to query set

2010-10-28 Thread owidjaya
If i have such models

class Project(model.Model):
  project = model.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.