Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django complex filter and order

I have 4 model like this

class Site(models.Model):
    name = models.CharField(max_length=200)

    def get_lowest_price(self, mm_date):
        '''This method returns lowest product price on a site at a particular date'''

class Category(models.Model):
    name = models.CharField(max_length=200)
    site = models.ForeignKey(Site)

class Product(models.Model):
    name = models.CharField(max_length=200)
    category = models.ForeignKey(Category)

class Price(models.Model):
    date = models.DateField()
    price = models.IntegerField()
    product = models.ForeignKey(Product)

Here every have many category, every category have many product. Now product price can change every day so price model will hold the product price and date.

My problem is I want list of site filter by price range. This price range will depends on the get_lowest_price method and can be sort Min to Max and Max to Min. Already I've used lambda expression to do that but I think it's not appropriate

sorted(Site.objects.all(), key=lambda x: x.get_lowest_price(the_date))

Also I can get all site within a price range by running a loop but this is also not a good idea. Please help my someone to do the query in right manner.

If you still need more clear view of the question please see the first comment from "Ishtiaque Khan", his assumption is 100% right.

*In these models writing frequency is low and reading frequency is high.

like image 859
Minuddin Ahmed Rana Avatar asked Aug 09 '17 07:08

Minuddin Ahmed Rana


1 Answers

1. Using query
If you just wanna query using a specific date. Here is how:

q = Site.objects.filter(category__product__price__date=mm_date) \
        .annotate(min_price=Min('category__product__price__price')) \
        .filter(min_price__gte=min_price, min_price__lte=max_price)

It will return a list of Site with lowest price on mm_date fall within range of min_price - max_price. You can also query for multiple date using query like so:

q = Site.objects.values('name', 'category__product__price__date') \
        .annotate(min_price=Min('category__product__price__price')) \
        .filter(min_price__gte=min_price, min_price__lte=max_price)

2. Eager/pre-calculation, you can use post_save signal. Since the write frequency is low this will not be expensive

  • Create another Table to hold lowest prices per date. Like this:
    class LowestPrice(models.Model):
        date = models.DateField()
        site = models.ForeignKey(Site)
        lowest_price = models.IntegerField(default=0)
  • Use post_save signal to calculate and update this every time there. Sample code (not tested)
    from django.db.models.signals import post_save
    from django.dispatch import receiver

    @receiver(post_save, sender=Price)
    def update_price(sender, instance, **kwargs):
        cur_price = LowestPrice.objects.filter(site=instance.product.category.site, date=instance.date).first()
        if not cur_price:
            new_price = LowestPrice()
            new_price.site = instance.product.category.site
            new_price.date = instance.date
        else:
            new_price = cur_price
        # update price only if needed
        if instance.price<new_price.lowest_price:
            new_price.lowest_price = instance.price
            new_price.save()
  • Then just query directly from this table when needed:
    LowestPrice.objects.filter(date=mm_date, lowest_price__gte=min_price, lowest_price__lte=max_price)
like image 87
Nathan Do Avatar answered Sep 22 '22 12:09

Nathan Do