Django 在显示时保留产品的顺序

发布于 2025-01-15 22:58:50 字数 1620 浏览 3 评论 0原文

假设我希望 Django 网上商店中的产品顺序按特定顺序显示。我怎样才能做到这一点?我简化了下面的示例来显示我遇到的问题。在我的网上商店模型中,我有以下 2 个模型。

class TakeawayWebshop(models.Model):
    name = models.CharField(max_length = 255, help_text = 'name of takeaway restaurant')
    products = models.ManyToManyField(Product)

class Product(models.Model):
    name = models.CharField(max_length = 50, unique=True)
    description = models.TextField()
    price = models.DecimalField(max_digits=9, decimal_places=0, default=0.00)

    class Meta:
        ordering = ['-created_at']

在 admin.py 中,我

class TakeawayWebshopAdmin(admin.ModelAdmin):
    form = TakeawayWebshopAdminForm
    list_display = ['name']
    ordering = ['name']
    filter_horizontal = ('products',)

admin.site.register(TakeawayWebshop, TakeawayWebshopAdmin)

现在从管理员那里得到了,我使用 filter_horizo​​ntal 按产品 A、产品 B 和产品 C 的精确顺序将 3 个产品添加到网上商店。 已添加产品确切的顺序。 ,filter_horizo​​ntal 框中的产品会自动重新排列,因此顺序变为产品 C、产品 B 和产品 A。

保存 TakeawayWebshop 模型对象后 .py 如果我得到我添加到网上商店的所有产品的列表,例如

webshop = TakeawayWebshop.objects.filter(name = 'my webshop name')[0]
products = webshop.products.all() 

返回的查询集是

<QuerySet [<Product: Product C>, <Product: Product B>, <Product: Product A>]>

显然我想我在尝试使查询集中的产品按以下顺序出现时使用了错误的策略我原本打算让他们成为将产品 A、产品 B 和产品 C 插入到 filter_horizo​​ntal 框中时进行排列。另外我想知道是否可以重新排列 filter_horizo​​ntal 框中产品的出现顺序?

Suppose I want the order of my products in my Django webshop to be displayed in a certain order. How can I achieve that? I have simplified the below example to show the problems that I am experiencing. In my webshop models I have the below 2 models.

class TakeawayWebshop(models.Model):
    name = models.CharField(max_length = 255, help_text = 'name of takeaway restaurant')
    products = models.ManyToManyField(Product)

class Product(models.Model):
    name = models.CharField(max_length = 50, unique=True)
    description = models.TextField()
    price = models.DecimalField(max_digits=9, decimal_places=0, default=0.00)

    class Meta:
        ordering = ['-created_at']

In the admin.py I have

class TakeawayWebshopAdmin(admin.ModelAdmin):
    form = TakeawayWebshopAdminForm
    list_display = ['name']
    ordering = ['name']
    filter_horizontal = ('products',)

admin.site.register(TakeawayWebshop, TakeawayWebshopAdmin)

Now from the admin, I am adding 3 products in the exact order of product A, product B and product C to the webshop using the filter_horizontal. product added the exact order. Upon saving the TakeawayWebshop model object, the products in the filter_horizontal box automatically rearanged, so the order becomes product C, product B and product A.

When from the views.py if I get a list of all the products I have added to the webshop such as

webshop = TakeawayWebshop.objects.filter(name = 'my webshop name')[0]
products = webshop.products.all() 

The queryset returned is

<QuerySet [<Product: Product C>, <Product: Product B>, <Product: Product A>]>

Obviously I guess I am using the wrong strategy in trying to make the products in query set appear in the order that I originally intend them to be arranged (product A, product B and product C) when inserting them into the filter_horizontal box. Also I am wondering if it is possible to rearrange the order that products in filter_horizontal box appear?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文