Thanks Euan, some great info there.

I'll have a dig through the docs for F objects, I wasn't even aware of
them so thanks for point me in that direction!

On 5 July, 10:35, "euan.godd...@googlemail.com"
<euan.godd...@gmail.com> wrote:
> I'd only use extra as a total last resort as it often results in poor
> portability between DB backends. That said if you're only ever going
> to stick to one you might not care.
>
> Django introduced F objects in 1.1. The documentation is not great and
> my experiments with it haven't been a massive success, but you might
> be able to achieve something like this with F (if you want to filter
> on being older than a certain number of days).
>
> Euan
>
> On Jul 5, 10:05 am, MikeHowarth <mikehowar...@googlemail.com> wrote:
>
> > You're right I sniped the date_time field when pasting in the model
> > after leaving out some extranous detail. Doh!
>
> > Ordering by date_time mean't I had duplicates despite grouping by
> > site. Like you say printing out the query may actually help me
> > understand what is going on under the hood.
>
> > After posting the message, I did a bit more reading I actually used
> > annotate(last_date=MAX('date_time')) which seems to work reasonably
> > well and give me the sort of result I was looking for. Again I'll
> > print the sql for this and see whats going on.
>
> > Extending the use case a bit further however what I'd really like to
> > do is determine how many days have passed since 'last date' and filter
> > the objects further. It sort of seems as though I'm getting to the
> > edge of what the ORM is capable of.
>
> > From doing a quick bit of reading it either seems like I'll have to
> > use the extra() clause or perhaps look at using a property bound to a
> > method however at this point you're outside the scope of the ORM.
>
> > Any ideas on how I can best acheive this?
>
> > I'm trying to stay away from using a foreach and looping over a
> > potentially large number of records and doing this filtering at the DB/
> > ORM level.
>
> > Thanks in advance.
>
> > On 5 July, 07:32, "euan.godd...@googlemail.com"
>
> > <euan.godd...@gmail.com> wrote:
> > > Erm. You don't seem to have specified what to order by. In this case,
> > > surely you want ... .order_by('date_time') However, this field doesn't
> > > seem to appear in your models.
>
> > > One thing I've found handy for debugging Django's ORM to SQL is
> > > something which I don't think is documented in django docs at all
> > > (although I maybe wrong about that) is doing:
>
> > > print qs.query
>
> > > Where qs is your queryset. It will output the SQL Django sends to the
> > > DB backend.
>
> > > Euan
>
> > > On 3 July, 15:03, MikeHowarth <mikehowar...@googlemail.com> wrote:
>
> > > > Hi all
>
> > > > Just coming back to Django after a long time away, and struggling to
> > > > get my head around what should be a trivial concept using the ORM.
>
> > > > Essentially I want to do acheive the following SQL:
>
> > > > SELECT * FROM publisher_history
> > > > INNER JOIN publisher_publisher ON publisher_publisher.id =
> > > > publisher_history.publisher_id
> > > > GROUP BY publisher_id ORDER BY date_time DESC;
>
> > > > My models look like this:
>
> > > > class Publisher(models.Model):
> > > >         name = models.CharField(max_length=100, blank=False)
>
> > > > class History(models.Model):
> > > >         publisher = models.ForeignKey(Publisher, blank=False)
>
> > > > I've been trying to use object values, but I'm getting duplicate
> > > > publishers. Code looks like this:
>
> > > > results = History.objects.values('publisher').distinct()
>
> > > > If I run:
>
> > > > results = History.objects.values('publisher').distinct().order_by()
>
> > > > I don't get duplicates but I don't get the results returned in the
> > > > order I expect either.
>
> > > > Any help would be greatly appreciated, I've stared at this for a while
> > > > now.

-- 
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.

Reply via email to