Django 高级搜索

Django advanced search

我目前正在通过制作一个销售二手自行车的网络应用程序来学习 Django,但我在现场搜索方面遇到了问题。 我想为每个模型字段都有一个搜索字段,但我不知道该怎么做。 最好的方法是什么? 任何帮助都非常受欢迎!

这是我的模型:

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

您可以使用 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)

并且您希望,如果您 select 一个大陆,则只有位于该大陆的国家可用,并且对于区域相同,您可以执行以下操作:

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)

我解决了这个但是我忘记了post答案以便有类似问题的任何人都可以使用它。它并不完美,但对我有用,如果有人有更好的解决方案,请随时回答。

在我的模型中我有:

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)

观看次数:

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