43

I was hoping to seek some assistance on this problem I'm having. I'm still learning Django (and Python) and come across this particular issue that I'm unable to locate an answer for. I've created a new App called "News" and setup the Model for the App. Using the Admin interface I have created some data. From my "Pages" App, I'm trying to import the News_Article class and getting the error No module named News.models.

I am struggling to see what's going wrong here.

Any assistance would be greatly appreciated.

DIR Structure

Bolton_GC [Folder]
- Bolton_GC [Folder]
  - News [Folder]
    - Migrations [Folder]
    - __init__.py
    - __init__.pyc
    - admin.py
    - admin.pyc
    - models.py
    - models.pyc
    - tests.py
    - views.py
  - Pages [Folder]
    - Migrations [Folder]
    - __init__.py
    - __init__.pyc
    - admin.py
    - admin.pyc
    - models.py
    - models.pyc
    - tests.py
    - views.py
    - views.pyc
  - static [Folder]
  - templates [Folder]
  - __init__.py
  - __init__.pyc
  - settings.py
  - settings.pyc
  - urls.py
  - urls.pyc
  - wsgi.py
  - wsgi.pyc
- db.sqlite3
- manage.py

news\model.py

from django.db import models
from datetime import datetime

class News_Article(models.Model):
    class Meta:
        ordering = ['news_datetime_submitted']
    news_title = models.CharField(max_length=75, verbose_name="News Title")
    news_text = models.CharField(max_length=300, verbose_name="News Text")
    news_active = models.BooleanField(default=True, verbose_name="News Active")
    news_datetime_submitted = models.DateTimeField(default=datetime.now(), verbose_name="News Date")

    def __str__(self):
        return self.news_title

Pages\views.py

from django.shortcuts import HttpResponse, get_object_or_404, render
from models import Page, Announcement, Menu, Sub_Menu
from django.core.exceptions import ObjectDoesNotExist
from News.models import News_Article
import pdb

# Helper Functions

def get_announcement():
    try:
        return Announcement.objects.get(announcement_active=True)
    except ObjectDoesNotExist:
        return None

def clean_url(dirtyurl, badlist):
    for item in badlist:
        dirtyurl = dirtyurl.replace(item,'')
    return dirtyurl[1:-1]

# View functions

def page(request):
    rDict = {}
    path = clean_url(request.path, ['"', "'"])
#    pdb.set_trace()
    p = get_object_or_404(Page, urlconf_text=path)
    rDict['p'] = p
    announcement = get_announcement()
    if not announcement == None:
        rDict['announcement'] = announcement
    rDict['sitenav'] = path
    rDict['menu'] = Menu.objects.all().order_by('menu_position')
    return render(request, 'en/public/page.html', rDict)

Error

ImportError at /home/

No module named News.models

Request Method:     GET
Request URL:    http://127.0.0.1:8000/home/
Django Version:     1.8.2
Exception Type:     ImportError
Exception Value:    

No module named News.models

Exception Location:     C:\Me\Websites\Bolton_GC\Bolton_GC\Pages\views.py in <module>, line 4
Python Executable:  c:\python27\python.exe
Python Version:     2.7.9
Python Path:    

['C:\\Me\\Websites\\Bolton_GC',
 'c:\\python27\\lib\\site-packages\\setuptools-18.0.1-py2.7.egg',
 'C:\\WINDOWS\\SYSTEM32\\python27.zip',
 'c:\\python27\\DLLs',
 'c:\\python27\\lib',
 'c:\\python27\\lib\\plat-win',
 'c:\\python27\\lib\\lib-tk',
 'c:\\python27',
 'c:\\python27\\lib\\site-packages']

Server time:    Tue, 14 Jul 2015 13:21:14 +0100
7
  • Interestingly it works in the urls.py file so it must be a folder structure issue but I'm stumped.
    – Smurf
    Jul 14, 2015 at 12:36
  • try using `Bolton_GC.News.models import News_Article' - I want to see if it's the structure Jul 14, 2015 at 12:44
  • Tried that also pal, same issue
    – Smurf
    Jul 14, 2015 at 12:49
  • Hmm, that does work, although it didn't. Thats annoying, sorry to bother you and thanks for the prompt reply.
    – Smurf
    Jul 14, 2015 at 12:51
  • So it does work? I have added an answer Jul 14, 2015 at 12:52

3 Answers 3

82

Switch

from News.models import News_Article

to

from Bolton_GC.News.models import News_Article
4
  • 27
    If you are using PyCharm, make sure to adjust the content root of your project from Preferences -> Project Structure
    – netcyrax
    Jun 10, 2017 at 12:19
  • 13
    @Mike building on that, you can set it by right-clicking on django's root folder in PyCharm (ie. the one that contains manage.py and go to Mark Directory As > Sources Root
    – Dash
    Jul 29, 2019 at 22:08
  • 6
    That does not work. I would get no module named Bolton_GC.
    – Soerendip
    Apr 19, 2021 at 18:01
  • 1
    from django.apps import apps....... member = apps.get_model('APP_NAME.MODEL_NAME'). This should fetch your model from another app and save it in the 'member' variable. Use member.objects.all() and any such ORM queries from this point on. link Apr 13, 2022 at 17:14
41

Just to elaborate on @TheLifeOfSteve's answer, all the import statements are always relative to your manage.py file.

If the manage.py file was at the path Bolton_GC/Bolton_GC, then the correct import statement would just be:

from News.models import News_Article

But in the current directory structure, the following is the correct answer as pointed out by Steve.

from Bolton_GC.News.models import News_Article
2

There is an update in Importing and registering Models!! Please try from .models import News_Article

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.