You should use 'F' expression from django.db.models
So your expression would be like:
ProductSale.objects.all().filter(date_expires__lt = now().date).exclude(
date_purchased__gt = F('date_expires'))
Which roughly translates to
SELECT * FROM productsale WHERE date_expires < CURRENT_DATE AND NOT
(date_purchased
> date_expires)
in mysql.
On Tuesday, 18 August 2015 06:26:56 UTC+5:30, Lee Hinde wrote:
>
> Given this model, I want to find all ProductSale objects that have expired
> and where the Person doesn't have a later (unexpired) purchase of the same
> product.
>
> class ProductSale(models.Model):
> product = models.ForeignKey(Product)
> person = models.ForeignKey(Person)
> ...
> date_expires = models.DateField(blank=True, db_index=True)
>
>
> The closest I've gotten is in the shell where I get a values_list of
> person and product for expired and unexpired and then compare the two
> lists. That leaves me with a list of tuples, that (due to the size of the
> list, I think) I can't use in a query.extra.
>
> Any suggestions would be appreciated.
>
> (and sorry for the lame subject. I couldn't think of a way to be both
> succinct and informative.)
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/26d0c805-54e5-4d9f-b3cb-07955cce9a66%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.