0π
β
At last solved it with a slight workaround:
class Product(models.Model):
name = models.CharField(max_length=255)
shops = models.ManyToManyField(Shop, through='ProductShop', related_name='products')
def lowest_possible_price(self):
shops = self.shops.all()
if not shops:
return "N/A"
return reduce(lambda x, y: min(x, ProductShop.objects.get(shop__pk=y.id, product__pk=self.id).price),
shops, ProductShop.objects.get(shop__pk=shops[0].id, product__pk=self.id).price)
products = sorted(Product.objects.all(),
key = lambda product : product.lowest_possible_price())
Still bothers me if thereβa one-line solution with no helper function (lowest_possible_price
here)
π€Wojtek
1π
You mean:
products = ProductShop.objects.filter(
shop__in=Shop.objects.filter()).annotate(Min(price)).order_by('price')
{% for ps in productshop %}
Shop: {{ ps.product.shop }}
Product: {{ ps.product }}
Minimum Price: {{ ps.price }}
{% endfor %}
π€catherine
- [Answer]-Django-registration with is_super_user on by default.
- [Answer]-Filter view from url parameter with an intermediate model?
- [Answer]-Redirect url in Django 1.4.5
- [Answer]-Web2py or Django: code example for web site that looks similar to the admin page
Source:stackexchange.com