質問

wishhing you a happy and a productive year. I have the following django project.

geoedu/
├── geoedu
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   └── wsgi.py
├── geoedu.sublime-project
├── geoedu.sublime-workspace
├── manage.py
├── school
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   └── views.py
└── student
    ├── __init__.py
    ├── __init__.pyc
    ├── models.py
    ├── models.pyc
    ├── tests.py
    └── views.py

(btw i love tree command). My school app has a class named School

class School(models.Model):
    #fields

and the student app has a model named Student which has a foreign key of schools. So in my school/models.py

from school.models import School

class Student(models.Model):
    #name and other personal data fields
    school = models.ForeignKey(School, related_name='school')

So a student belongs only to one school but a certain school can have many students. But when trying to execute the sqlall command to see if everything is working fine, i get an import error

./manage.py sqlall school
ImportError: cannot import name School

and

./manage.py sqlall student
ImportError: cannot import name School

If i comment out the foreign key field and the import everything works fine. Why isn't it seeing the import? All project folder was created from terminal using the django-admin command and the apps using the manage command from scratch.

The sublime project file is the following

{
    "folders":
    [
        {
           "follow_symlinks": true,
           "path": "."
        }
   ]
}
役に立ちましたか?

解決

The reason is that you've got imports loop in your models structure. Using the model's name "school.School" for ForeignKey should fix this. Or you could rethink your models' design.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top