Define an order for ManyToManyField with Django

后端 未结 5 701
清酒与你
清酒与你 2020-12-13 02:15

Is there a way to define the order of objects related through a ManyToManyField?

Example:

  • ArticleContainer1 contains in this order :
    article

5条回答
  •  孤街浪徒
    2020-12-13 02:39

    On Django 2.0, the accepted answer of Francis Yaconiello works well with the exception of a warning caused by the max_length argument for the order field of class DepartmentPeople.

    Django ignores max_length for integer fields, and will warn you in Django 1.8+.

    I would add a unique_together entry in the Meta subclass

    class Meta:
        #...
        unique_together = ['person', 'department', 'order']
    

    to prevent data entry errors.

提交回复
热议问题