如何在Django中按ModelMultipleChoiceFeild创建排序

时间:2018-09-06 03:00:40

标签: python django sorting django-models django-forms

我是python和编码的新手,想知道是否有人对我有用。我正在使用Django创建一个电子商务网站,我希望允许用户轻松地将所有产品从高到低,从低到高,新产品等进行分类。 I would like the field to look similar to this:

我已经在网站上创建了按颜色,产品类型和尺寸进行过滤的过滤器。

我的models.py看起来像这样:

class e_info(models.Model):

    product = models.CharField(max_length=120)
    description = models.TextField(default="desctription default text")
    price = models.IntegerField(default=0)

    product_group = models.ForeignKey(ProductGroup, on_delete=models.SET_NULL, null=True)
    #sub_product_group = models.ForeignKey(SubProductGroup, on_delete=models.CASCADE)
    size = models.ForeignKey(Size, on_delete=models.SET_NULL, null=True)
    color = models.ForeignKey(Color, on_delete=models.SET_NULL, null=True)

    image = models.FileField(upload_to= 'post_image', blank=True)
    image_1 = models.FileField(upload_to='post_image', blank=True)
    image_2 = models.FileField(upload_to='post_image', blank=True)
    image_3 = models.FileField(upload_to='post_image', blank=True)
    image_4 = models.FileField(upload_to='post_image', blank=True)
    date_added = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name_plural= 'e_info'

    def __str__(self):
        return self.product[:50]

我的看法如下:

def about(request):
    about = e_info.objects.all()
    product = ProductGroup.objects.all()
    color = Color.objects.all()
    info_search = e_info.objects.all().order_by('-price')
    info_filter = ColorFilter(request.GET, queryset=info_search)
    context = {
        'about': about,
        'color': color,
        'product': product,
        'info_filter': info_filter,
    }
    template = 'about.html'
    return render(request, template, context)

我想对e_info模型中的信息进行排序。任何帮助将不胜感激。谢谢!

0 个答案:

没有答案