在SQL或Django ORM中,常规的方式是使用一对多的订单? [英] in SQL, or Django ORM, what's the conventional way to have an ordered one-to-many?

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

问题描述

说我想要一个项目,一对一的待办事项,想要任意重新排序待办事项?



过去,我添加了一个编号的订单字段,当有人想要更改订单时,不得不使用新的订单号更新所有项目。这可能是最糟糕的做法,因为它不是原子和需要几个更新。



我注意到Django有一个多值的CommaSeparatedIntegerField,它可以通过将订购的密钥存储在待办事项表中的项目中来包含该顺序项目表的一个领域。



我已经考虑了一个清除十进制系统,如果我想把项目3放在1和2之间,我会改变它的顺序数字为1.5。



有些东西告诉我有一个更简单的选择,我失踪了...



你如何命令一对多的关系?

解决方案

我讨厌这个问题...我遇到对于我最近的Django网站,我们有一个包含N个文章的通讯,当然,订单很重要。我将默认订单分配为递增的Article.id,但如果文章以正确的顺序输入,则失败。



在电子邮件change_form.html页面I使用Interface插件添加了一些jQuery magic( http://interface.eyecon.ro/ )。我显示相关文章的标题,用户可以随意拖动它们。有一个onChange处理程序重新计算article_id的article_order字段。



享受,

Peter



对于app = content,model = Newsletter,以下是
templates / admin / content / newslettter / change_form.html

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

{%block form_top%} {%endblock%}
{%block extrahead%} {{ block.super}}
< script type =text / javascriptsrc =/ media / js / jquery.js>< / script>
< script type =text / javascriptsrc =/ media / js / interface.js>< / script>
< script>
$(document).ready(
function(){
$('ol.articles')。可排序(
{
accept:'sortableitem',
helperclass:'sorthelper',
activeclass:'sortableactive',
hoverclass:'sortablehover',
opacity:0.8,
fx:200,
轴:'vertical',
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 art){
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>相关文章< / h4>
< ol class =articlesid =article_list>
{%for original.articles%}
< li id =article。{{art.id}}class =sortableitem> {{art.title}}< /立GT;

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


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.

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.

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.

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.

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.

Enjoy,
Peter

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天全站免登陆