我有5个类,CustomerSupplierOrderDesignOutSource
Order具有3个关系,与Customer一对一,与Design一对多,与OutSourse一对一。首先,我只是添加了s one to one relationship with Customer (customer=models.ForeignKey(Customer)), now I wanna add the those 2 relationships with Design and Outsourse`。

 design=models.ManyToManyField(Design)
 outSource=models.OneToOneField(OutSource)


当我这样做并运行“ makemigrations”命令时,出现此错误->“ NameError:名称“ Design”未定义”
没有这两行代码,我可以毫无问题地进行迁移...无法弄清楚我要去哪里。
任何帮助将不胜感激。

models.py

class Order(models.Model):
    o_type=models.CharField(max_length=15, verbose_name='Order type')
    number=models.IntegerField()
    date=models.DateField()
    status=models.CharField(max_length=25)
    delivery_date=models.DateField()
    customer=models.ForeignKey(Customer)
    design=models.ManyToManyField(Design)
    outSource=models.OneToOneField(OutSource)

class Design(models.Model):
    dimension=models.IntegerField()
    image=models.ImageField(upload_to='images/%Y/%m/%d')
    number_of_colors=models.IntegerField()
    sides=models.IntegerField(verbose_name='side(s)')

class OutSource(models.Model):
    date=models.DateField()
    number=models.IntegerField()
    description=models.CharField(max_length=100)
    code=models.IntegerField()
    supplier=models.ForeignKey(Supplier)

最佳答案

在实际定义Design关系之后,您要声明类ManyToManyField。它找不到名称为Design的类,因此也找不到NameError

在班级Design之前声明班级Order,一切对您都应该起作用。

关于python - Django models.py中的NameError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32091252/

10-13 02:29