简体   繁体   English

使用 django-admin 界面订购商品

[英]Order items with the django-admin interface

Lets say I have a django model looking like this:假设我有一个 django 模型,如下所示:

class question(models.Model):
  order = models.IntegerField('Position')
  question = models.CharField(max_length= 400)
  answer = models.TextField()
  published = models.BooleanField()

  def __unicode__(self):
    return self.question

In my view I show all of the questions ordered ascending by the order field.在我看来,我显示了按顺序字段升序排列的所有问题。

My question is: Is there an easy way to edit the order field in the django admin interface?我的问题是:有没有一种简单的方法可以在 django 管理界面中编辑订单字段? Right now, I have to go to edit the Question, then look up what number to put in the order field and maybe even reorder all the other items.现在,我必须去编辑问题,然后查找要在订单字段中输入的数字,甚至可能重新排序所有其他项目。 What i really want would be some "up and down"-arrows on the admin page where all the questions are listed.我真正想要的是列出所有问题的管理页面上的一些“上下”箭头。

Is that possible?那可能吗?

Check this: django-orderedmodel .检查这个: django-orderedmodel

This is a really simple implementation of abstract base class for items which can be ordered with admin interface.这是可以通过管理界面订购的项目的抽象基类的非常简单的实现。 No external dependencies and easy to use.没有外部依赖,易于使用。

Sure, here is an example of admin.py file with up and down links to change items order: https://github.com/alexvasi/django-simplemenu/blob/master/simplemenu/admin.py当然,这是一个 admin.py 文件的示例,其中包含用于更改项目顺序的上下链接: https ://github.com/alexvasi/django-simplemenu/blob/master/simplemenu/admin.py

Basically, you just need to override get_urls method to add your custom views ( move_up and move_down in this example).基本上,您只需要覆盖get_urls方法来添加您的自定义视图(在本例中为move_upmove_down )。

More famous example would be django-treemenus , but there is some extra code to support older versions of django.更著名的例子是django-treemenus ,但是有一些额外的代码来支持旧版本的 django。

万一其他人在 2017 年寻求该问题的解决方案,我发现了很棒的包Django Admin Sortable

您可以使用django-admin-sortable2轻松更改项目的顺序,包括内联项目。

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

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