-1

I have a Teacher model and a Academic model. When i import something in academic model from teacher model Then I found a Import Error like

File "/media/sajib/Work/Project/schoolmanagement/teacher/models.py", line 3, in from academic.models import Department, ClassInfo, Section, Session, Shift File "/media/sajib/Work/Project/schoolmanagement/academic/models.py", line 2, in from teacher.models import GuideTeacher ImportError: cannot import name 'GuideTeacher' from 'teacher.models' (/media/sajib/Work/Project/schoolmanagement/teacher/models.py)

Now how can i solve this? academic/models.py

from teacher.models import GuideTeacher
class ClassRegistration(models.Model):
    department_select = (
        ('general', 'General'),
        ('science', 'Science'),
        ('business', 'Business'),
        ('humanities', 'Humanities')
    )
    department = models.CharField(choices=department_select, max_length=15, null=True)
    class_name = models.ForeignKey(ClassInfo, on_delete=models.CASCADE, null=True)
    section = models.ForeignKey(Section, on_delete=models.CASCADE, null=True)
    session = models.ForeignKey(Session, on_delete=models.CASCADE, null=True)
    shift = models.ForeignKey(Shift, on_delete=models.CASCADE, null=True)
    guide_teacher = models.OneToOneField(GuideTeacher, on_delete=models.CASCADE, null=True)
    date = models.DateField(auto_now_add=True)

    class Meta:
        unique_together = ['class_name', 'section', 'shift', 'guide_teacher']

    def __str__(self):
        return str(self.class_name)

teacher/models.py

from academic.models import Department, ClassInfo, Section, Session, Shift
class GuideTeacher(models.Model):
    name = models.OneToOneField(PersonalInfo, on_delete=models.CASCADE, null=True)
    date = models.DateField(auto_now_add=True)

    def __str__(self):
        return str(self.name)

1 Answer 1

1

This is because of Circular Import. To get rid of the situation, remove the import statement in academic/models.py and specify FK related model using string representation

guide_teacher = models.OneToOneField('teacher.GuideTeacher', on_delete=models.CASCADE, null=True)
3
  • Have you removed the line from teacher.models import GuideTeacher from academic/models.py file?
    – JPG
    Jan 15, 2020 at 6:09
  • 1
    use 'teacher.GuideTeacher' (which is a string -- use single quotes or double quotes)
    – JPG
    Jan 15, 2020 at 6:52
  • 1
    You can remove the "probably" from the answer. Jan 15, 2020 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.