0

I'm trying to import a model into some app from another app, but it throws an error.

enter image description here

from django.db import models
from ..book.models import Book


class Cart:
    books = models.ManyToManyField(Book)

Error:

ValueError: attempted relative import beyond top-level package

But if I do:

from django.db import models
from book.models import Book


class Cart:
    books = models.ManyToManyField(Book)

PyCharm will be complaining. It says "Unsolved reference book".

This won't work as well:

from django.db import models
from shop.book.models import Book


class Cart:
    books = models.ManyToManyField(Book)

Error:

ModuleNotFoundError: No module named 'shop.book'
SpellTheif
  • 715
  • 2
  • 12
  • 26

3 Answers3

0

You should try importing with a function. def make_it_im_port(self): import(x) Then call the function.

0

Make sure book exists as an app in the INSTALLED_APPS list within settings.py

pfcodes
  • 1,055
  • 2
  • 9
  • 15
0

Your code would work fine when you use the below. It is a pycharm issue and you can solve the pycharm issue by doing this

from django.db import models
from book.models import Book 
class Cart:
    books = models.ManyToManyField(Book)
Arghya Saha
  • 5,599
  • 4
  • 26
  • 48