Ivan Pilipovic
Ivan Pilipovic

Reputation: 121

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: ')

Upvotes: 3

Views: 2078

Answers (2)

Ivan Pilipovic
Ivan Pilipovic

Reputation: 121

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})

Upvotes: 3

dan-klasson
dan-klasson

Reputation: 14180

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)

Upvotes: 0

Related Questions