在SQL或Django ORM中,有序一对多的常规方法是什么? [英] in SQL, or Django ORM, what's the conventional way to have an ordered one-to-many?

查看:50
本文介绍了在SQL或Django ORM中,有序一对多的常规方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我想有一个项目,并且有多个待办事项,并且想任意重新订购待办事项?

Say I wanted to have a project, and one-to-many with to-do items, and wanted to re-order the to-do items arbitrarily?

过去,我添加了一个带编号的订单字段,当有人要更改订单时,必须使用新的订单号更新所有商品.这可能是最糟糕的方法,因为它不是原子&需要几次更新.

In the past, I've added a numbered order field, and when someone wants to change the order, had to update all the items with their new order numbers. This is probably the worst approach, since it's not atomic & required several updates.

我注意到Django具有一个多值CommaSeparatedIntegerField,它可以通过将项目的有序键存储在项目表的一个字段中的待办事项表中来包含该顺序.

I notice Django has a multi-valued CommaSeparatedIntegerField which could contain the order by storing the ordered keys to the items in the to-do items table right in one field of the project table.

我已经考虑了一个杜威十进制系统,如果我想将第3项放在1到2之间,我会将其订单号更改为1.5.

I've pondered a dewey decimal system where if I wanted to take item 3 and put it between 1 and 2 I would change it's order number to 1.5.

某事告诉我,还有一个更简单的选项,尽管我不见了...

Something tells me there's an easier option that I'm missing though...

您如何下达一对多关系的订单?

How would you give order to a one-to-many relationship?

推荐答案

我讨厌这个问题……而且我一直都遇到它.

I hate this problem ... and I run into it all the time.

对于我最近的Django网站,我们有一个新闻通讯,其中包含N条文章,当然,顺序很重要.我将默认顺序分配为升序Article.id,但如果输入的文章不是正确"顺序,则失败.

For my most recent Django site we had a Newsletter which contained N Articles and, of course, order was important. I assigned the default order as ascending Article.id, but this failed if Articles were entered in something other than "correct" order.

在Newsletter change_form.html页面上,我使用了Interface插件( http://interface.eyecon.ro/).我显示了相关文章的标题,用户可以随意拖动它们.有一个onChange处理程序可以重新计算article.id中article_order字段中的内容.

On the Newsletter change_form.html page I added a little bit of jQuery magic using the Interface plugin (http://interface.eyecon.ro/). I show the titles of the associated Articles and the user can drag them around as they like. There is an onChange handler that recomputes the Article.id's in article_order field.

享受,
彼得

对于app = content,model = Newsletter,以下内容为template/admin/content/newslettter/change_form.html

For app=content, model=Newsletter, the following is in templates/admin/content/newslettter/change_form.html

{% extends 'admin/change_form.html' %}

{% block form_top %}{% endblock %}
{% block extrahead %}{{ block.super }}
<script type="text/javascript" src="/media/js/jquery.js"></script>
<script type="text/javascript" src="/media/js/interface.js"></script>
<script>
$(document).ready(
    function () {
        $('ol.articles').Sortable(
            {
                accept :        'sortableitem',
                helperclass :   'sorthelper',
                activeclass :   'sortableactive',
                hoverclass :    'sortablehover',
                opacity:        0.8,
                fx:             200,
                axis:           'vertically',
                opacity:        0.4,
                revert:         true,
                trim:           'art_',
                onchange:
                    function(list){
                        var arts = list[0].o[list[0].id];
                        var vals = new Array();
                        var a;
                        for (a in arts) {
                            vals[a] = arts[a].replace(/article./, '');
                        }
                        $('#id_article_order').attr('value', vals.join(','));
                    }
            });
    }
);
</script>
{% endblock %}

{% block after_related_objects %}
{% if original.articles %}
<style>
.sortableitem {
    cursor:move;
    width: 300px;
    list-style-type: none;
    }
</style>

<h4>Associated Articles</h4>
<ol class="articles" id="article_list">
{% for art in original.articles %}
    <li id="article.{{art.id}}" class="sortableitem">{{art.title}}</li>

{% endfor %}
</ol>
{% endif %}
{% endblock %}

这篇关于在SQL或Django ORM中,有序一对多的常规方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆