Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django advanced search

I am currently learning Django by making a web app that sell used bikes and I'm having problems with on site search. I would like to have a search field for every model field and I just can't figure out how to do it. What would be the best way to do this? Any help is more than welcome!

Here is my model:

class UsedBike(models.Model):
manufacturers = (
    ('Aprilia', 'Aprilia'),
    ('Benelli', 'Benelli'),
    ('BMW', 'BMW'),
    ('Cagiva', 'Cagiva'),
    ('Gilera', 'Gilera'),
    ('Harley-Davidson', 'Harley-Davidson'),
    ('Husaberg', 'Husaberg'),
    ('Husquarna', 'Husquarna'),
    ('Hyosung', 'Hyosung'),
    ('Kawasaki', 'Kawasaki'),
    ('KTM', 'KTM'),
    ('Kymco', 'Kymco'),
    ('Moto Guzzi', 'Moto Guzzi'),
    ('MV Agusta', 'MV Agusta'),
    ('Suzuki', 'Suzuki'),
    ('Tomos', 'Tomos'),
    ('Triumph', 'Triumph'),
    ('Yamaha', 'Yamaha'),
    )
manufacturer = models.CharField(help_text = 'Manufacturer: ', 
                                max_length = 20,
                                choices = manufacturers)
model = models.CharField(max_length = 20, help_text = 'Model: ')
like image 357
Ivan Pilipovic Avatar asked Feb 12 '26 03:02

Ivan Pilipovic


1 Answers

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})
like image 75
Ivan Pilipovic Avatar answered Feb 13 '26 16:02

Ivan Pilipovic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!