3๐
โ
I solved this but I forgot to post the answer so that anyone who have the similar problem can use it. It is not perfect but it worked for me, if someone have a better solution feel free to answer.
In my models I have:
class UsedBike(models.Model):
manufacturer = models.CharField(max_length = 20)
model = models.CharField(max_length = 20)
year = models.PositiveIntegerField(default = 0)
bike_type = models.CharField(max_length = 20)
price = models.PositiveIntegerField(default = 0)
engine_size = models.PositiveIntegerField(default = 0)
And in views:
def searchbike(request):
man = request.GET.get('manufacturer')
mod = request.GET.get('model')
year_f = request.GET.get('year_from')
year_t = request.GET.get('year_to')
price_f = request.GET.get('price_from')
price_t = request.GET.get('price_to')
bike_t = request.GET.get('bike_type')
capacity_f = request.GET.get('cubic_capacity_from')
capacity_t = request.GET.get('cubic_capacity_to')
question_set = UsedBike.objects.filter()
if request.GET.get('manufacturer'):
question_set = question_set.filter(manufacturer__exact = man)
if request.GET.get('model'):
question_set = question_set.filter(model__icontains = mod)
if request.GET.get('year_from'):
question_set = question_set.filter(year__gte = year_f)
if request.GET.get('year_to'):
question_set = question_set.filter(year__lte = year_t)
if request.GET.get('price_from'):
question_set = question_set.filter(price__gte = price_f)
if request.GET.get('price_to'):
question_set = question_set.filter(price__lte = price_t)
if request.GET.get('bike_type'):
question_set = question_set.filter(bike_type__exact = bike_t)
if request.GET.get('cubic_capacity_from'):
question_set = question_set.filter(engine_size__gte = capacity_f)
if request.GET.get('cubic_capacity_to'):
question_set = question_set.filter(engine_size__lte = capacity_t)
return render(request, 'shop/search.html', { 'searched': question_set})
๐คIvan Pilipovic
0๐
You can use django-smart-selects:
If you have the following model:
class Location(models.Model)
continent = models.ForeignKey(Continent)
country = models.ForeignKey(Country)
area = models.ForeignKey(Area)
city = models.CharField(max_length=50)
street = models.CharField(max_length=100)
And you want that if you select a continent only the countries are available that are located on this continent and the same for areas you can do the following:
from smart_selects.db_fields import ChainedForeignKey
class Location(models.Model)
continent = models.ForeignKey(Continent)
country = ChainedForeignKey(
Country,
chained_field="continent",
chained_model_field="continent",
show_all=False,
auto_choose=True
)
area = ChainedForeignKey(Area, chained_field="country", chained_model_field="country")
city = models.CharField(max_length=50)
street = models.CharField(max_length=100)
๐คdan-klasson
- [Django]-Making text bold with django
- [Django]-Django โ Sort the list into 3 columns on templates
- [Django]-How do I properly install django registration on localhost?
- [Django]-Django โ Is it possible to make a redirect from a function in a view?
- [Django]-Wordpress & Django โ One domain, two servers. Possible?
Source:stackexchange.com