Django:如何从同一个应用程序导入模型?



我有一个名为lists的应用程序。

这有3个主要模型:ListListItemSchool

每个列表可能与 1 所学校相关,或者此字段可以为空。

但是当我尝试更新我的模型列表以拥有学校字段时,我得到了:

ImportError: cannot import name 'School' from 'lists.models' (D:web_proyectsscolartelistsmodels.py)  
(scolarte) 

甚至认为两个模型都在同一个models.py文件中。

我试过:

from .models import School

和:

from lists.models import School

列表/模型.py:

from django.db import models
from products.models import Product
from roles.models import User
from .models import School
# Create your models here.
class List(models.Model):
LISTA_STATUS = (
('recibida_pagada', 'Recibida y pagada'),
('recibida_no_pagada', 'Recibida pero no pagada'),
('en_revision', 'En revision'),
('en_camino', 'En camino'),
('entregada', 'Entregada'),
('cancelada', 'Cancelada')
)
lista_id = models.CharField(max_length=100)
name = models.CharField(max_length=100)
user = models.OneToOneField(User, on_delete=models.CASCADE)
school = models.OneToOneField(School, on_delete=models.CASCADE)
status = models.CharField(max_length=20, choices=LISTA_STATUS, default='recibida_pagada')
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['created_at']
def __str__(self):
return str(self.id)

class ListItem(models.Model):
lista = models.ForeignKey(List, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
comment = models.CharField(max_length=100, blank=True, null=True, default='')
uploaded_at = models.DateTimeField(auto_now_add=True)
step_two_complete = models.BooleanField(default=False)
def sub_total(self):
return int(self.product.price)

class School(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=100, blank=False)
address_reference = models.CharField(max_length=100, blank=False)
provincia = models.CharField(max_length=100, blank=False, null=True)
canton = models.CharField(max_length=100, blank=False, null=True)
parroquia = models.CharField(max_length=100, blank=False, null=True)
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['created_at']
def __str__(self):
return str(self.name)

您不需要导入School,因为它已经在同一模型中。但是,您确实需要先定义School然后才能在同一模型文件的另一个类中引用它。更新模型,以便在List之前定义School

from django.db import models
from products.models import Product
from roles.models import User
from .models import School
# Create your models here.
class School(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=100, blank=False)
address_reference = models.CharField(max_length=100, blank=False)
provincia = models.CharField(max_length=100, blank=False, null=True)
canton = models.CharField(max_length=100, blank=False, null=True)
parroquia = models.CharField(max_length=100, blank=False, null=True)
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['created_at']
def __str__(self):
return str(self.name)

class List(models.Model):
LISTA_STATUS = (
('recibida_pagada', 'Recibida y pagada'),
('recibida_no_pagada', 'Recibida pero no pagada'),
('en_revision', 'En revision'),
('en_camino', 'En camino'),
('entregada', 'Entregada'),
('cancelada', 'Cancelada')
)
lista_id = models.CharField(max_length=100)
name = models.CharField(max_length=100)
user = models.OneToOneField(User, on_delete=models.CASCADE)
school = models.OneToOneField(School, on_delete=models.CASCADE)
status = models.CharField(max_length=20, choices=LISTA_STATUS, default='recibida_pagada')
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['created_at']
def __str__(self):
return str(self.id)

class ListItem(models.Model):
lista = models.ForeignKey(List, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
comment = models.CharField(max_length=100, blank=True, null=True, default='')
uploaded_at = models.DateTimeField(auto_now_add=True)
step_two_complete = models.BooleanField(default=False)
def sub_total(self):
return int(self.product.price)

最新更新