Hi,

I have an example application of the shop.

Full application code is available at 
https://github.com/dry-python/tutorials/tree/master/django

In this example, User can buy a subscription for different categories of
the content.

The user can subscribe for one day, one month or one year.

Each period has its own price.

Price can change in time, so Price model stores not the actual prices
but the whole Price history.

The Price with the most recent from_date value is the actual one.

This is the Price model.

class Price(models.Model):

    category = models.ForeignKey(Category, related_name="prices", on_delete=
models.CASCADE)

    from_date = models.DateField()

    cost = models.DecimalField(max_digits=10, decimal_places=2)

    period = models.IntegerField()


I want to select the cheapest (from actual) price for each category.

I can express this with following SQL query.

SELECT "category_id",
       MIN("cost")
  FROM "example_price"
 WHERE "id" IN (
   SELECT "id"
     FROM "example_price"
    WHERE "category_id" IN (1, 2, 3, 4)
    GROUP BY "category_id", "period"
   HAVING "from_date" = MAX("from_date")
 )
 GROUP BY "category_id"

How I can express it with Django ORM?

Regards, Artem.

-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/32f37c0c-2038-4a7f-97c8-b1d4d85dbbbf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to