I
I
irvinddz2015-03-27 16:12:43
Django
irvinddz, 2015-03-27 16:12:43

Searching for a model by price, if the price can be specified in currency?

Hello.
There is a model:

class Flat(models.Model):
    ...
    price = models.IntegerField('Цена в рублях/долларах/евро')
    price_currency = models.CharField('Валюта')
    pub_date = models.DateTimeField('Дата публикации')
    ...

At the front there is a search form with a minimum and maximum price in rubles . We will assume that we have the exchange rate. You need to write a query to select all apartments that fall within a price range and sort them by publication date. I started working with Django recently, tell me how to implement it correctly.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oscar Django, 2015-03-27
@irvinddz

from django.db.models import Q
queryset = Flat.objects.filter(
Q(price_currency='RUB', price__lte=price_max, price__gte=price_min) | 
Q(price_currency='USD', price__lte=price_max*rate_usd, price__gte=price_min*rate_usd) | 
Q(price_currency='EUR', price__lte=price_max*rate_eur, price__gte=price_min*rate_eur)
)

V
Vadim Shandrinov, 2015-03-27
@suguby

queryset = Flat.objects.filter(price__lte=200, price__gte=100).order_by('pub_date')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question