Re: order_by not working with foreign keys:

2007-02-02 Thread Ramiro Morales

Hi Michael,

On 2/2/07, Michael Radziej <[EMAIL PROTECTED]> wrote:
>
> Hi Ramiro,
>
> are you the Mr. Anonymous who left comment #18? Just for
> clarification that I understand who is who ;-)
>

No I'm not :), it seems to me Mr Anonymous is Bram Dejong. I attached
the updated
patch to ticket #2076.

Regards,

-- 
 Ramiro Morales

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-02-02 Thread Michael Radziej

Hi Ramiro,

are you the Mr. Anonymous who left comment #18? Just for
clarification that I understand who is who ;-)

Michael


-- 
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49-911-9352-0 - Fax +49-911-9352-100

http://www.noris.de - The IT-Outsourcing Company

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-02-02 Thread Ramiro Morales

On 2/1/07, Russell Keith-Magee <[EMAIL PROTECTED]> wrote:
>
> On 2/1/07, Michael Radziej <[EMAIL PROTECTED]> wrote:
> > > is there a
> > > HOWTO somewhere on how to write a test which does the whole model,
> > > database sync, etc etc cycle?
> > [...]
>
> [...]
>
> Django's internal tests are in the /tests directory, and are executed
> by running /tests/runtests.py. /tests/modeltests are tests that double
> as documentation (they get automagically turned into the online
> documentation at http://djangoproject.com/documentaion/models/);
> /tests/regressiontests contains more extensive tests, and tests to
> make sure that a particular bug never surfaces again.
>

I've tried to implement some tests of this functionality.

As I see, James Bennett and serbaut <[EMAIL PROTECTED]> (the original author of
the patch) are right when they [1]say the #2210 ticket is about a
different issue and
that the symptoms described in this thread make ticket #2076 a better
match. I may
have some responsability because I was the first to suggest these two
tickets were
related :(

So, I have attached the patch to ticket #2076 and IMHO it should to be
reopened, ticket #2210 has become very confusing and some effort should be put
in trying to reproduce the missbehaviour described by the original poster.

The patch includes the modifications implemented by serbaut and
enhanced by Michael
Radziej updated to trunk as of r4455 (just line offset
unfuzzyfication) and modifications
to the "ordering" modeltest/example (tests/modeltets/ordering/).

Some notes:

* The patch allows one to use relationship_name__related_model_field_name
  ordering specification in the order_by() QuerySet method but not in the
  Meta.ordering attribute.
* I have tested only ForeignKey relationships.
* I have not tested if the ordering specification can be used by
spanning through reverse
  relationships.
* The patch does not seem to preserve backward compatibility with the "old"
  '[-]appname_tablename.fieldname' order_by specification notation
suggested by the
  [2]Django documentation, would it be worth to note this if the patch
gets applied?.

HTH

1. 
http://groups.google.com/group/django-developers/browse_frm/thread/731f26c0184f6cde/28181488c9dea079?lnk=gst&rnum=2&hl=en#28181488c9dea079

2. http://www.djangoproject.com/documentation/db_api/#order-by-fields

-- 
 Ramiro Morales

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-02-01 Thread Russell Keith-Magee

On 2/1/07, Michael Radziej <[EMAIL PROTECTED]> wrote:
> > is there a
> > HOWTO somewhere on how to write a test which does the whole model,
> > database sync, etc etc cycle?
>
> http://www.djangoproject.com/documentation/testing/
>
> explains how tests and fixtures work. Look for an existing test for
> this and extend it.

Just to clarify - the testing documentation refers to tests that you
write for your own projects (i.e., unit testing your own Django
application). The Django internal tests use the same doctest and
unittest framework as your own application tests, but are executed
using an internal testing framework.

Django's internal tests are in the /tests directory, and are executed
by running /tests/runtests.py. /tests/modeltests are tests that double
as documentation (they get automagically turned into the online
documentation at http://djangoproject.com/documentaion/models/);
/tests/regressiontests contains more extensive tests, and tests to
make sure that a particular bug never surfaces again.

Yours,
Russ Magee %-)

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-02-01 Thread Michael Radziej

Bram - Smartelectronix:
> Michael Radziej wrote:
>> Bram - Smartelectronix:
>>
>>
>>> I'll be updating the ticket, but I was wondering when the ticket would 
>>> be merged with trunk.
>> Someone needs to writes tests for this. As soon as this has been
>> done, the ticket can be promoted to "ready for checkin", which means
>> added core developer attention ;-)
> 
> I would be a prime candidate for writing this test, I guess

Not necessarily. It's not trivial, but we really appreciate if you
take the time to do it.

Just to warn you, it still isn't a guarantee that the patch will be
applied soon, that's completely dependent on the committers and
their schedule, which I have no information about. If you really
really need it soon, I'd recommend to patch your Django
installations in the meantime for yourself, and watch carefully how
it behaves after an update.

> is there a 
> HOWTO somewhere on how to write a test which does the whole model, 
> database sync, etc etc cycle?

http://www.djangoproject.com/documentation/testing/

explains how tests and fixtures work. Look for an existing test for
this and extend it.

It shouldn't be too difficult to find out how to organize the tests
by looking at the existing tests. There are two main categories, one
for tests that may be used as examples, too, and the other
("regression tests") that sort out all the hard edge cases and that
would be too hairy as example.

If you have a problem and still want to contribute the tests (which
is very welcome!), don't hesitate to ask, better on the developer list.

Michael


-- 
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49-911-9352-0 - Fax +49-911-9352-100

http://www.noris.de - The IT-Outsourcing Company

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-02-01 Thread Bram - Smartelectronix

Michael Radziej wrote:
> Bram - Smartelectronix:
> 
> 
>> I'll be updating the ticket, but I was wondering when the ticket would 
>> be merged with trunk.
> 
> Someone needs to writes tests for this. As soon as this has been
> done, the ticket can be promoted to "ready for checkin", which means
> added core developer attention ;-)

I would be a prime candidate for writing this test, I guess, is there a 
HOWTO somewhere on how to write a test which does the whole model, 
database sync, etc etc cycle?


  - bram

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-02-01 Thread Michael Radziej

Bram - Smartelectronix:


> I'll be updating the ticket, but I was wondering when the ticket would 
> be merged with trunk.

Someone needs to writes tests for this. As soon as this has been
done, the ticket can be promoted to "ready for checkin", which means
added core developer attention ;-)

Michael


-- 
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49-911-9352-0 - Fax +49-911-9352-100

http://www.noris.de - The IT-Outsourcing Company

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-02-01 Thread Bram - Smartelectronix

Michael Radziej wrote:
> Hi,
> 
> there's a ticket with a patch about the order_by-Problem:
> 
> http://code.djangoproject.com/ticket/2210.
> 
> Does it help?

I just figured out that this problem also exists, even if the name ISN'T 
different:

class Book(models.Model):
title = models.TextField()

class BookStat(models.Model):
book = models.OneToOneField(Book)
times_read = models.PositiveIntegerField()

Book.objects.all().order_by("bookstat__times_read")

gives the same error(s). So it's not just for relations with other 
names, but any relation defined in another table.


I'll be updating the ticket, but I was wondering when the ticket would 
be merged with trunk.


  - bram

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-01-26 Thread Bram - Smartelectronix

Michael Radziej wrote:
> Please report your results in the ticket, I'd appreciate!

Done.

  - bram

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-01-25 Thread Michael Radziej

Bram - Smartelectronix:
> Michael Radziej wrote:
>> Hi,
>>
>> there's a ticket with a patch about the order_by-Problem:
>>
>> http://code.djangoproject.com/ticket/2210.
>>
>> Does it help?
> 
> Knowing it's broken helps a LOT :-)
> I'll try to see if it works w/ the patch.

Please report your results in the ticket, I'd appreciate!

Michael


-- 
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49-911-9352-0 - Fax +49-911-9352-100

http://www.noris.de - The IT-Outsourcing Company

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-01-25 Thread Bram - Smartelectronix

Michael Radziej wrote:
> Hi,
> 
> there's a ticket with a patch about the order_by-Problem:
> 
> http://code.djangoproject.com/ticket/2210.
> 
> Does it help?

Knowing it's broken helps a LOT :-)
I'll try to see if it works w/ the patch.

Thanks Michael,


  - bram

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-01-25 Thread Michael Radziej

Hi,

there's a ticket with a patch about the order_by-Problem:

http://code.djangoproject.com/ticket/2210.

Does it help?


Michael

-- 
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49-911-9352-0 - Fax +49-911-9352-100

http://www.noris.de - The IT-Outsourcing Company

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-01-25 Thread Bram - Smartelectronix

Waylan Limberg wrote:
> Actually, as you have `related_name` set (to 'stats'), I believe this
> may be what you want:
> 
> Book.objects.all().select_related().order_by('-stats__avg_rating')[0:10]

OK, as this failed as well, I made a clean example, from ZERO, and tried 
it. Any ideas why this would fail would be great ;-)

Here goes:

models.py
-

from django.db import models

class Book(models.Model):
title = models.TextField()

class BookStat(models.Model):
book = models.OneToOneField(Book, related_name='stats')
times_read = models.PositiveIntegerField()


After syncing the DB, in the command line:
--

(
saving 1 book + stat, then trying:
Book.objects.all().order_by("stats__times_read")
)

 >>> from orderby.example.models import *
 >>> book = Book.objects.create(title="BFG")
 >>> book.save()
 >>> bs = BookStat.objects.create(book=book, times_read=15)
 >>> bs.save()
 >>> Book.objects.all().order_by("stats__times_read")
Traceback (most recent call last):
   File "", line 1, in 
   File "C:\Python25\lib\site-packages\django\db\models\query.py", line 
101, in __repr__
 return repr(self._get_data())
   File "C:\Python25\lib\site-packages\django\db\models\query.py", line 
444, in _get_data
 self._result_cache = list(self.iterator())
   File "C:\Python25\lib\site-packages\django\db\models\query.py", line 
181, in iterator
 cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + 
",".join(select) + sql, params)
   File "C:\Python25\lib\site-packages\django\db\backends\util.py", line 
12, in execute
 return self.cursor.execute(sql, params)
   File 
"C:\Python25\lib\site-packages\django\db\backends\postgresql\base.py", 
line 43, in execute
 return self.cursor.execute(sql, [smart_basestring(p, self.charset) 
for p in params])
ProgrammingError: ERROR:  column example_book.stats__times_read does not 
exist at character 80
SELECT "example_book"."id","example_book"."title" FROM "example_book" 
ORDER BY "example_book"."stats__times_read" ASC

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-01-24 Thread Waylan Limberg

On 1/24/07, Bram - Smartelectronix <[EMAIL PROTECTED]> wrote:
>
> Waylan Limberg wrote:
> > On 1/24/07, Bram - Smartelectronix <[EMAIL PROTECTED]> wrote:
> >>  Book.objects.all().select_related()
> >>  .order_by('-bookshop_bookstat.avg_rating')[0:10]
> >>
> >
> > You should replace the dot (.) with a double underscore (__), so:
> >
> > .order_by('-bookshop_bookstat__avg_rating')[0:10]
>
> Sadly enough no go either, this gives me:
> "column bookshop_book.bookstat__avg_rating does not exist"
>
Hmm, you shouldn't have any dots in there, and this looks different
that your earlier examples. Perhaps you should check the docs [1].

I should also note that in the example I gave, I did not look at you
models. Now that I have, it seems you are confused more than I
originally thought. You don't need to refer to each  model, just
follow the relationship with double underscores. try:


Book.objects.all().select_related().order_by('-book__avg_rating')[0:10]

Actually, as you have `related_name` set (to 'stats'), I believe this
may be what you want:


Book.objects.all().select_related().order_by('-stats__avg_rating')[0:10]

[1]: 
http://www.djangoproject.com/documentation/db_api/#lookups-that-span-relationships


-- 

Waylan Limberg
[EMAIL PROTECTED]

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-01-24 Thread Bram - Smartelectronix

Waylan Limberg wrote:
> On 1/24/07, Bram - Smartelectronix <[EMAIL PROTECTED]> wrote:
>>  Book.objects.all().select_related()
>>  .order_by('-bookshop_bookstat.avg_rating')[0:10]
>>
> 
> You should replace the dot (.) with a double underscore (__), so:
> 
> .order_by('-bookshop_bookstat__avg_rating')[0:10]

Sadly enough no go either, this gives me:
"column bookshop_book.bookstat__avg_rating does not exist"

cheers + thanks for the continuing help

  - bram

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-01-24 Thread Waylan Limberg

On 1/24/07, Bram - Smartelectronix <[EMAIL PROTECTED]> wrote:
>
>  Book.objects.all().select_related()
>  .order_by('-bookshop_bookstat.avg_rating')[0:10]
>

You should replace the dot (.) with a double underscore (__), so:

.order_by('-bookshop_bookstat__avg_rating')[0:10]


-- 

Waylan Limberg
[EMAIL PROTECTED]

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-01-24 Thread Bram - Smartelectronix

Honza Král wrote:
>> -
>> class Book(models.Model):
>>  
>>
>> class BookStat(models.Model):
>>  book = models.OneToOneField(Song, related_name='stats')
>>  avg_rating = models.FloatField(max_digits=2, decimal_places=1)
>>  ...
>> -
> 
> those two models aren't related, so even if you do select_related()
> book_stat still wouldn't get joined in...
> How do you want to sort Books by BookStats when there is no relation
> between them? Or is there something I am missing?

Bad edit from my side... The model should have read:

book = models.OneToOneField(Book, related_name='stats')


  - bram

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-01-24 Thread Honza Král
On 1/24/07, Bram - Smartelectronix <[EMAIL PROTECTED]> wrote:
>
> Honza Kr�l wrote:
> > it occured before on the mailing list...
> >
> > if you add select_related(), it will work...
> >
> > the problem is that specifying ordering like this doesn't force the
> > join in the query so you might end up with a query that sorts on
> > something that just isn't there...
>
> That didn't work either:
>
>  Book.objects.all().select_related()
>  .order_by('-bookshop_bookstat.avg_rating')[0:10]
>
> still gives me:
>
>  ERROR: missing FROM-clause entry for table "bookshop_bookstat"
>
>
> Perhaps I'm doing something else wrong. These are my models:
>
> -
> class Book(models.Model):
>  
>
> class BookStat(models.Model):
>  book = models.OneToOneField(Song, related_name='stats')
>  avg_rating = models.FloatField(max_digits=2, decimal_places=1)
>  ...
> -

those two models aren't related, so even if you do select_related()
book_stat still wouldn't get joined in...
How do you want to sort Books by BookStats when there is no relation
between them? Or is there something I am missing?

>
> We're doing this because the statistics are calculated in cron jobs.
>
> Btw, what if a certain book doesn't have statistics (yet)? Should I be
> dropping back into SQL? :-)
>
>
>   - bram
>
> >
>


-- 
Honza Kr�l
E-Mail: [EMAIL PROTECTED]
ICQ#:   107471613
Phone:  +420 606 678585

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-01-24 Thread Bram - Smartelectronix

Honza Král wrote:
> it occured before on the mailing list...
> 
> if you add select_related(), it will work...
> 
> the problem is that specifying ordering like this doesn't force the
> join in the query so you might end up with a query that sorts on
> something that just isn't there...

That didn't work either:

 Book.objects.all().select_related()
 .order_by('-bookshop_bookstat.avg_rating')[0:10]

still gives me:

 ERROR: missing FROM-clause entry for table "bookshop_bookstat"


Perhaps I'm doing something else wrong. These are my models:

-
class Book(models.Model):
 

class BookStat(models.Model):
 book = models.OneToOneField(Song, related_name='stats')
 avg_rating = models.FloatField(max_digits=2, decimal_places=1)
 ...
-

We're doing this because the statistics are calculated in cron jobs.

Btw, what if a certain book doesn't have statistics (yet)? Should I be 
dropping back into SQL? :-)


  - bram

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: order_by not working with foreign keys:

2007-01-23 Thread Honza Král
it occured before on the mailing list...

if you add select_related(), it will work...

the problem is that specifying ordering like this doesn't force the
join in the query so you might end up with a query that sorts on
something that just isn't there...

On 1/23/07, Bram - Smartelectronix <[EMAIL PROTECTED]> wrote:
>
> Hello Everyone,
>
>
> Trying this:
>
> Book.objects.all().order_by('-app_statistics.avg_rating')
>
> I get:
>
> ProgrammingError at /bleep/
> ERROR: missing FROM-clause entry for table "app_statistics"
> at character 375
>
> Anyone know what the problem is? (working with trunk, updated today). I
> saw some tickets related to this but they all seemed rather old.
>
>
> thanks!
>
>
>   - bram
>
> >
>


-- 
Honza Kr�l
E-Mail: [EMAIL PROTECTED]
ICQ#:   107471613
Phone:  +420 606 678585

--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---