Django高级搜索

时间:2015-02-06 00:35:56

标签: python django 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: ')

2 个答案:

答案 0 :(得分:3)

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

在我的模特中,我有:

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

答案 1 :(得分:0)

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

如果您选择一个大陆,只有那些位于该大陆的国家/地区可用,您可以执行以下操作:

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)