【问题标题】:Re-ordering entries in a model using drag-and-drop使用拖放重新排序模型中的条目
【发布时间】:2010-09-11 18:22:35
【问题描述】:

假设我在 Django 中有一个博客应用程序。如何在默认管理员中使用可拖动表格重新排序帖子?

如果我不必在模型中添加任何额外的字段,那将是最好的,但如果我真的需要,我可以。

【问题讨论】:

  • 对于 2011 年的 django 1.2/1.3,现在最好的解决方案是什么?

标签: javascript python django


【解决方案1】:

只有在模型中添加字段才能确定顺序。让我们在模型中添加位置字段。

In your models.py 

class MainModel(models.Model):

    name = models.CharField(max_length=255)
    position = models.PositiveSmallIntegerField(null=True)

    class Meta:
        ordering = ('position',)

In your apps admin.py

class IdeaCardTagInline(nested_admin.NestedStackedInline):
    model = MainModel
    extra = 0
    min_num = 1
    sortable_field_name = "position"

Meta 中的ordering = ('position',) 将根据位置字段中的值对 MainModel 进行排序。 sortable_field_name = "position" 将有助于在用户拖放多个模型时自动填充位置值。

要在您的 api 中查看相同的订单,请在 views_model.py 中使用 order_by(x)。 只是一个模型示例:

Model.objects.filter().order_by('position').

【讨论】:

    【解决方案2】:

    你可以试试这个 sn-p https://gist.github.com/barseghyanartur/1ebf927512b18dc2e5be(最初基于这个 sn-p http://djangosnippets.org/snippets/2160/)。

    集成非常简单。

    您的模型(在 models.py 中):

    class MyModel(models.Model):
        name = models.CharField(max_length=255)
        order = models.IntegerField()
        # Other fields...
    
        class Meta:
            ordering = ('order',)
    

    你的管理员(admin.py):

    class MyModelAdmin(admin.ModelAdmin):
        fields = ('name', 'order',) # Add other fields here
        list_display = ('name', 'order',)
        list_editable = ('order',)
    
        class Media:
            js = (
                '//code.jquery.com/jquery-1.4.2.js',
                '//code.jquery.com/ui/1.8.6/jquery-ui.js',
                'path/to/sortable_list.js',
            )
    

    如果您负责保存名称和排序的属性在模型中的名称不同,请相应地更改(sn-p 的)JS。

    【讨论】:

      【解决方案3】:

      现在有一个不错的包用于订购管理对象

      https://github.com/iambrandontaylor/django-admin-sortable

      另外,django-suit 内置了对此的支持

      【讨论】:

      • 不幸的是 django-suit 的实现不是拖放。
      【解决方案4】:

      有关执行此操作的工作代码,请查看snippet 1053djangosnippets.org

      【讨论】:

        【解决方案5】:

        注意“如果我不必在模型中添加任何额外的字段,那将是最好的,但如果我真的必须这样做,我可以。”

        抱歉,数据库中信息的顺序是由信息本身决定的:您总是需要添加一列进行排序。对此真的别无选择。

        此外,要按此顺序检索事物,您需要专门将 .order_by(x) 添加到您的查询中或将 ordering 添加到您的模型中。

        class InOrder( models.Model ):
            position = models.IntegerField()
            data = models.TextField()
            class Meta:
                ordering = [ 'position' ]
        

        没有额外的字段排序就不会发生。这是关系数据库的规则之一。

        【讨论】:

        • 谢谢,与作者相反,我正在寻找要添加的行!
        【解决方案6】:

        在模型类中,您可能必须添加“订单”字段,以维护特定订单(例如,订单 = 10 的项目是最后一个,订单 = 1 是第一个)。然后你可以在 admin change_list 模板中添加一个 JS 代码(参见this)来维护拖放功能。最后将模型 Meta 中的排序更改为 ['order'] 之类的内容。

        【讨论】:

          猜你喜欢
          • 2014-04-20
          • 1970-01-01
          • 2013-05-11
          • 1970-01-01
          • 1970-01-01
          • 2018-01-25
          • 2016-11-13
          • 2021-09-13
          • 1970-01-01
          相关资源
          最近更新 更多