繁体   English   中英

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

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

在我的模型中,我有:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM