Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 如何按父级显示类别顺序?_Django_Django Models - Fatal编程技术网

Django 如何按父级显示类别顺序?

Django 如何按父级显示类别顺序?,django,django-models,Django,Django Models,我有一个模型: class Category(models.Model): parent = models.ForeignKey('self', null=True, blank=True) name = models.CharField(max_length=200) class Meta: db_table = 'categories' ordering = ['parent'] 但在管理方面,我得到: Infinite loop

我有一个模型:

class Category(models.Model):
    parent = models.ForeignKey('self', null=True, blank=True)
    name = models.CharField(max_length=200)

    class Meta:
        db_table = 'categories'
        ordering = ['parent']
但在管理方面,我得到:

Infinite loop caused by ordering.
如何显示具有父级排序的类别

我想按如下方式对类别进行排序:

category 1
children of category 1
children of category 1
children of category 1 (category 1a)
children of category 1a
children of category 1
category 2
children of category 2
children of category 2
... etc

ordering=['parent']
更改为
ordering=['parent\uuu id']
好的,这是可行的,但我想按父项排序:父项(子项,子项),下一个父项(子项,父项),子项等等。您能更准确地解释一下您想要实现的排序吗?