[Solved] Attempted relative import beyond toplevel package in django


Try this in your models:

#Remove the import statement: from blog.models import sighinmodel
#Then, inside your model
user = models.ForeignKey('blog.sighinmodel' , on_delete = None)

Also, I would like to point out that this is not the correct way of importing other modules in your models.py .

You should do like this:

from appname.models import ModelName
#for importing from another module's models.

There is no need for relative path names in import statements in Django. from appname.module import function/class works fine for nearly all the cases until cyclic redundancy occurs, in which you have to take one among many methods. One is the way I mentioned above:

Method 1: Simply put this inside the ModelClass. Don’t import anything.

user = models.ForeignKey('blog.sighinmodel' , on_delete = None)

Method 2(when cyclic import condition is not arising)

from blog.models import sighinmodel
class SomeModel(models.Model): 
    user = models.ForeignKey(sighinmodel , on_delete = None)

NOTE: The above will work only if a cyclic import isn’t occurring. In case the cyclic import condition is occurring, switch back to the first method of declaration.

Hope this helps. Thanks.

solved Attempted relative import beyond toplevel package in django