Re: [M-R] order by on custom SELECT column

2006-05-02 Thread Adrian Holovaty

On 5/1/06, Cheng Zhang <[EMAIL PROTECTED]> wrote:
> I use a custom SELECT column as:
> select = {
> 'choices': 'SELECT COUNT(*) FROM polls_choice WHERE
> poll_id=polls_poll.id',
> }
> p = Poll.objects.extra(select=select).order_by('choices')
>
> By trying this, I found out a bug in the ORM layer which I reported
> and submitted a patch as:
> http://code.djangoproject.com/ticket/1730
>
> Custom SELECT column is one way, I'd like to know is there any other
> ways to get the same thing, esp. only using db api since I try to
> avoid doing custom SQL as much as possible for portability purpose.

Hi Cheng,

There's no cleaner way to get the same thing, but I think that would
be a fine addition. Maybe something like
Poll.objects.populate_related_aggregates('choice', 'count').get(id=3).
Syntax would be the main thing to figure out here.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



[M-R] order by on custom SELECT column

2006-05-01 Thread Cheng Zhang

On the models of tutorial 1, I want to get a list of Poll, order by  
how many choices it has.

class Poll(models.Model):
 question = models.CharField(maxlength=200)
 pub_date = models.DateTimeField('date published')

class Choice(models.Model):
 poll = models.ForeignKey(Poll)
 choice = models.CharField(maxlength=200)
 votes = models.IntegerField()

I use a custom SELECT column as:
select = {
'choices': 'SELECT COUNT(*) FROM polls_choice WHERE  
poll_id=polls_poll.id',
}
p = Poll.objects.extra(select=select).order_by('choices')

By trying this, I found out a bug in the ORM layer which I reported  
and submitted a patch as:
http://code.djangoproject.com/ticket/1730

Custom SELECT column is one way, I'd like to know is there any other  
ways to get the same thing, esp. only using db api since I try to  
avoid doing custom SQL as much as possible for portability purpose.

BR,
- Cheng


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---