简体   繁体   中英

Inter app model import in Django

File tree image

I'm struggling with inter app model importing in Django

I've had a look at others who have asked the same sort of question and the answers have not satisfied my problem

my file structure is:

welted_website

-cart
--views

-shop
--models

There's a clickable link to the full file tree image at the top also

I'm trying to import Product from my shop.models into cart.views but to no avail!

I've tried

from ..shop.models import Product

from welted_website.shop.models import Product 

The second of which gave me the following:

ValueError: Attempted relative import beyond toplevel package 

My apps contain __init__.py files by default

shop and cart have been added to INSTALLED_APPS

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'shop',
'cart',

)

Install the apps in your settings

INSTALLED_APPS = (
    ...
    'shop',
)

import Product

from shop.models import Product 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM