in SQL, or Django ORM, what's the conventional way to have an ordered one-to-many?

前端 未结 5 656
暗喜
暗喜 2021-02-06 07:12

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, a

5条回答
  •  無奈伤痛
    2021-02-06 07:51

    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 }}
    
    
    
    {% endblock %}
    
    {% block after_related_objects %}
    {% if original.articles %}
    
    
    

    Associated Articles

      {% for art in original.articles %}
    1. {{art.title}}
    2. {% endfor %}
    {% endif %} {% endblock %}

提交回复
热议问题