List Info

Thread: How to 'order_by' on a FK in a list of objects




How to 'order_by' on a FK in a list of objects
country flaguser name
United States
2007-10-21 09:15:24
Hello,
In my view I do some queries on a class called Style.  The
end result
is I have a variable called styles that contains a list of
Style
objects.  For example:

styles = [<Style: 42k>, <Style: 51c>, <Style:
561j>

///////////////////

Below are my Style and Collection classes:

class Style(models.Model):
    name = models.CharField(maxlength=200, core=True)
    collection = models.ForeignKey(Collection)

class Collection(models.Model):
    name = models.CharField(maxlength=200)
    value = models.IntegerField()

////////////////////

My problem is that I want to be able to order my styles list
by the
'value' field from my Collection class.  In my code I try to
do:

styles.order_by('collection.value')

However, I get the error: 'OperationalError: no such
column:
collection.value'.

Does anybody know how I can order my styles variable by my
'value'
field in my Collection class?

Thanks


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: How to 'order_by' on a FK in a list of objects
country flaguser name
Australia
2007-10-21 09:21:02
On Sun, 2007-10-21 at 07:15 -0700, Greg wrote:
[...]
> My problem is that I want to be able to order my styles
list by the
> 'value' field from my Collection class.  In my code I
try to do:
> 
> styles.order_by('collection.value')
> 
> However, I get the error: 'OperationalError: no such
column:
> collection.value'.
> 
> Does anybody know how I can order my styles variable by
my 'value'
> field in my Collection class?

http://groups.google.com/group/dj
ango-users/browse_frm/thread/e79a1424f6519b9b#

Regards,
Malcolm

-- 
Everything is _not_ based on faith... take my word for it. 
http://www.pointy-s
tick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: How to 'order_by' on a FK in a list of objects
country flaguser name
United States
2007-10-21 09:38:08
Malcolm,
The Django documentation states:

To order by a field in a different table, add the other
table's name
and a dot, like so:
Entry.objects.order_by('blogs_blog.name', 'headline')

(I'm not sure what the headline attribute is doing)

////////////

Isn't that the same as what I'm doing?

styles.order_by('collection.value')



On Oct 21, 9:21 am, Malcolm Tredinnick <malc...pointy-stick.com>
wrote:
> On Sun, 2007-10-21 at 07:15 -0700, Greg wrote:
>
> [...]
>
> > My problem is that I want to be able to order my
styles list by the
> > 'value' field from my Collection class.  In my
code I try to do:
>
> > styles.order_by('collection.value')
>
> > However, I get the error: 'OperationalError: no
such column:
> > collection.value'.
>
> > Does anybody know how I can order my styles
variable by my 'value'
> > field in my Collection class?
>
> http://groups.google.com/group/django-us
ers/browse_frm/thread/e79a142...
>
> Regards,
> Malcolm
>
> --
> Everything is _not_ based on faith... take my word for
it.http://www.pointy-s
tick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: How to 'order_by' on a FK in a list of objects
country flaguser name
Australia
2007-10-21 09:42:58
On Sun, 2007-10-21 at 07:38 -0700, Greg wrote:
> Malcolm,
> The Django documentation states:
> 
> To order by a field in a different table, add the other
table's name
> and a dot, like so:
> Entry.objects.order_by('blogs_blog.name', 'headline')
> 
> (I'm not sure what the headline attribute is doing)
> 
> ////////////
> 
> Isn't that the same as what I'm doing?
> 
> styles.order_by('collection.value')

No, because the database table isn't called
"collection". It'll be
called something like appname_collection. Have a look in
your database
or at the value of Collection._meta.db_table.

Regards,
Malcolm

-- 
Success always occurs in private and failure in full view. 
http://www.pointy-s
tick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: How to 'order_by' on a FK in a list of objects
country flaguser name
United States
2007-10-21 09:50:18
Malcolm,
I looked in my db and my table is called plush_collection. 
However,
I've also tried

styles.order_by('plush_collection.value')

and I still get the same error 'OperationalError: no such
column:
plush_collection.value'



On Oct 21, 9:42 am, Malcolm Tredinnick <malc...pointy-stick.com>
wrote:
> On Sun, 2007-10-21 at 07:38 -0700, Greg wrote:
> > Malcolm,
> > The Django documentation states:
>
> > To order by a field in a different table, add the
other table's name
> > and a dot, like so:
> > Entry.objects.order_by('blogs_blog.name',
'headline')
>
> > (I'm not sure what the headline attribute is
doing)
>
> > ////////////
>
> > Isn't that the same as what I'm doing?
>
> > styles.order_by('collection.value')
>
> No, because the database table isn't called
"collection". It'll be
> called something like appname_collection. Have a look
in your database
> or at the value of Collection._meta.db_table.
>
> Regards,
> Malcolm
>
> --
> Success always occurs in private and failure in full
view.http://www.pointy-s
tick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: How to 'order_by' on a FK in a list of objects
country flaguser name
Australia
2007-10-21 09:56:10
On Sun, 2007-10-21 at 07:50 -0700, Greg wrote:
> Malcolm,
> I looked in my db and my table is called
plush_collection.  However,
> I've also tried
> 
> styles.order_by('plush_collection.value')
> 
> and I still get the same error 'OperationalError: no
such column:
> plush_collection.value'

Well, do the standard stuff and see what SQL is being
produced (see the
FAQ if you don't know how to do that) and see if that makes
it any
clearer. Do note, though, as I mentioned in the original
thread I linked
to and the bug reports linked from there, this isn't an area
that is
100% robust.

Malcolm

-- 
How many of you believe in telekinesis? Raise my hand... 
http://www.pointy-s
tick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: How to 'order_by' on a FK in a list of objects
country flaguser name
United States
2007-10-21 10:22:52
Malcolm,
I don't know much about how to see what SQL is being
produced.  This
is probably something I should learn.  Where is the FAQ
located at?

On Oct 21, 9:56 am, Malcolm Tredinnick <malc...pointy-stick.com>
wrote:
> On Sun, 2007-10-21 at 07:50 -0700, Greg wrote:
> > Malcolm,
> > I looked in my db and my table is called
plush_collection.  However,
> > I've also tried
>
> > styles.order_by('plush_collection.value')
>
> > and I still get the same error 'OperationalError:
no such column:
> > plush_collection.value'
>
> Well, do the standard stuff and see what SQL is being
produced (see the
> FAQ if you don't know how to do that) and see if that
makes it any
> clearer. Do note, though, as I mentioned in the
original thread I linked
> to and the bug reports linked from there, this isn't an
area that is
> 100% robust.
>
> Malcolm
>
> --
> How many of you believe in telekinesis? Raise my
hand...http://www.pointy-s
tick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: How to 'order_by' on a FK in a list of objects
country flaguser name
Australia
2007-10-21 10:35:26
On Sun, 2007-10-21 at 08:22 -0700, Greg wrote:
> Malcolm,
> I don't know much about how to see what SQL is being
produced.  This
> is probably something I should learn.  Where is the FAQ
located at?

Oh, come on! How hard did you look? It's the first hit on
Google for
"Django FAQ", there's also a search button on the
docs page on the
Django website.

Please spend a few minutes trying to answer your own
question before
posting to the list! It might mean people are more willing
to help when
you have a real question.

Malcolm

-- 
Why be difficult when, with a little bit of effort, you
could be
impossible. 
http://www.pointy-s
tick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: How to 'order_by' on a FK in a list of objects
country flaguser name
United States
2007-10-22 13:26:34
Malcolm,
Thanks for pointing me in the right direction.  I looked at
the Django
FAQ's and read about 'connection.queries'.  Using that I was
able to
look at what SQL statements  were being generated.  From
that I
noticed that in the FROM clause my table plush_collection
was not
being called.  That is why I was getting the error 'no such
column
plush_collection.value'.

To fix this problem in my filter statement's I added
select_related().  So I used

 styles = styles.select_related().filter(etc...)

instead of

styles = styles.filter(etc...)

Once I looked at the SQL of that query then I was able to
get
information from the different FK's.  I also read that
using
select_related() is also a good (In most cases) technique to
use to
speed up queries.  I might include depth=1 just to make sure
that I
don't go too many levels down.

Thanks

On Oct 21, 10:35 am, Malcolm Tredinnick <malc...pointy-stick.com>
wrote:
> On Sun, 2007-10-21 at 08:22 -0700, Greg wrote:
> > Malcolm,
> > I don't know much about how to see what SQL is
being produced.  This
> > is probably something I should learn.  Where is
the FAQ located at?
>
> Oh, come on! How hard did you look? It's the first hit
on Google for
> "Django FAQ", there's also a search button on
the docs page on the
> Django website.
>
> Please spend a few minutes trying to answer your own
question before
> posting to the list! It might mean people are more
willing to help when
> you have a real question.
>
> Malcolm
>
> --
> Why be difficult when, with a little bit of effort, you
could be
> impossible.http://www.pointy-s
tick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-9]

about | contact  Other archives ( Real Estate discussion Medical topics )