djangodjango-modelsdjango-rest-framework

cannot import name in django rest framework


Structure of app

enter image description here

areas models.py

from django.db import models
from products.models import ProductModel

class ProductionLine(models.Model):
    name = models.CharField(max_length = 50, unique=True)

products models.py

from django.db import models
from areas.models import ProductionLine

class ProductModel(models.Model):
    name= models.CharField(max_length = 255, unique=True)
    productionLine = models.ForeignKey(ProductionLine,on_delete=models.CASCADE)


I'm trying to import ProductionLine, but when i import ProductionLine i have an error :

And when i delete the import of ProductionLine in products.models, everything works. I don't understand


Solution

  • here you import ProductionLine in products models.py and also import ProductModel in areas.py, which causes circular dependency, in this case, you can use model string instead:

    from django.db import models
    # comment this line
    # from areas.models import ProductionLine
    
    class ProductModel(models.Model):
        name= models.CharField(max_length = 255, unique=True)
        productionLine = models.ForeignKey('areas.ProductionLine',on_delete=models.CASCADE)