--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
It is clearly a superior query :/
Cheers
Tom
I'm curious, why can't I talk you into
Student.objects.all().order_by('-score')[0] ?
It is clearly a superior query :/
On Tue, Oct 26, 2010 at 10:55 AM, Piotr Kilczuk <kil...@gmail.com> wrote:
>
>
> 2010/10/26 Tom Evans <teva...@googlemail.com>
>>
>> I'm curious, why can't I talk you into
>> Student.objects.all().order_by('-score')[0] ?
>>
>> It is clearly a superior query :/
>
> This would select only one row; there can possibly be multiple students with
> a top note.
>
> Am I right? :)
>
> Regards,
> Piotr
>
If you already know precisely the query you want to use, and you can't
coerce django's ORM to produce it, can you simply use Manager.raw()[1]
to generate the result set you are after?
Eg,
Student.objects.raw(r'SELECT * FROM `student` WHERE mark=(select
max(mark) from student)')
Cheers
Tom
[1] http://docs.djangoproject.com/en/1.2/topics/db/sql/
I hope the auditors are only forcing you to do this with records that
aren't referenced as part of relationships, otherwise your database is
going to get hammered updating all the foreign keys.
Wouldn't it make more sense (not that auditors will necessarily be
persuaded by sensible arguments) to dump a copy of a row (plus possibly
a timestamp field) to an archival table before update? This coild easily
be done on a pre-save signal ...
regards
Steve
--
DjangoCon US 2010 September 7-9 http://djangocon.us/
This sounds like what django-reversion[1] does :)
[1]: http://github.com/etianen/django-reversion#readme
--
Łukasz Rekucki
Regarding this query, I think you may be able to do this using
annotate. See http://docs.djangoproject.com/en/dev/topics/db/aggregation/#values
For example (and this probably sucks for performance):
Things.objects.filter(id__in=Things.objects.values('name').annotate(max_id=Max('id')).values_list('max_id',
flat=True))
This is just a self join example, but it could probably be rewritten
to use the two tables in your example.