按字母顺序排序Django models.py:参考下面定义的模型

时间:2017-04-17 13:48:52

标签: python django models

快速提问,

我试图清理那些有点遍布的models.py;认为按字母顺序排序会很聪明......

问题某些模型会引用其他模型(通过外键),这些模型现在已在文件中定义

我确定这很容易解决,我该怎么办?

from django.db import models


class Craft(models.Model):
    subdpt = models.ForeignKey(Subdepartment, on_delete=models.CASCADE)
    #### UNDEFINED NAME SUBDEPARTMENT ###

    title = models.CharField(max_length = 200)
    description = models.CharField(max_length = 200, blank = True)

    def __str__(self):
        return self.title

class Subdepartment(models.Model):
    subdpt_name = models.CharField(max_length=200)

    def __str__(self):
        return self.subdpt_name

    class Meta:
        permissions = (
            ("can_add", "Can add items in app"),
            ("can_edit", "Can edit items in app"),
        )

1 个答案:

答案 0 :(得分:3)

您始终可以将相关型号名称​​指定为字符串

subdpt = models.ForeignKey('Subdepartment', on_delete=models.CASCADE)