Django Import Model Error - django

I have Created an App inside my project folder, in the same directory where manage.py exists.
i have also created model for my app, when i import it in python shell like Import Poll
it gives this error:
ImportError: No module named poll

Supposing your app name is 'App' you have to type:
from App.models import Poll
in order to successfully import your Poll model. Change App with your current application name
Also, two things to bear in mind
1) Poll is different from poll: check that your capitalization is consistent with your model definition
2) You should have created the app using django startapp command. If not, at least make sure you have an empty init.py file inside your App folder.

Related

Unable to import models from the local python file

I have installed flask and flask-sqlalchemy in my base environment of the conda. I defined the models in separate file (model.py), when I am trying to import it in my app.py file it's throwing the following error
When you write from model import User, you cause python to import from app import db, i.e. import app into app, which is not allowed in python
Please, take a look at this answer, I wrote it pretty straightforward how to solve this problem with flask-sqlalchemy:
https://stackoverflow.com/a/62991785/10468419

Django Apps aren't loaded yet: How to import models

I have a Django project looking like
> project
> gui
> __init__.py
> models.py
> views.py
> ...
> project
__init__.py
...
I am trying to sync the sqllite db in django with some info I periodically query from other sources. So in project.init.py I spawn a thread that periodically queries data.
However, I am having trouble accessing my models from there and update the database, because when I try to import them into init.py
from gui.models import GuiModel
I get
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Is there a trick to do that or a different way to create a separate thread?
From the Django Official Doc, If you’re using components of Django “standalone” you should follow something like this,
import sys
import os
import django
sys.path.append("/path/to/project") # here project is root folder(means parent).
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "store.settings")
django.setup()
from gui.models import GuiModel
# do something here with models
If you send all details correctly, I think you have a circular import in your code. simple way is to to move import into your function.
also you can create a custom command in your project and add a cronjob to your server to do this works.

How to keep out utility functions in a separate directory inside an app in Django

In my current project, I have moved out most of the business logic from my views and have put them under the following directory structure myapp/utility/utility.py. Under the Utility directory i have put the init.py file also. I am using most of the models from my Models.py file in the utils.py file. Now the problem I am facing is when I run python manage.py runserver, it throws up an error saying models not found.
I had also followed the steps listed in the Django docs here. The project structure can be seen below. The following are just below my app directory.
In the utils.py I am trying to do the import
from my_project.myapp.models import Location, Product
This where it is not working.
In Django you may import from project's root directory directly, so in your case it should be something like this:
from myapp.models import Location, Product

Unable to import models in __init__.py django 1.9.4

My directory structure is.
Mypack:
--> __init__.py
--> admin.py
--> apps.py
--> foo.py
--> models.py
In apps.py, I have AppConfig. I have some methods in foo.py which uses the models imported from models.py. and I imported all methods in init.py.
from foo import *
I make this app installable with pip. when I install it in other django app and try to run python manage.py check. it gives the following error.
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
please suggest me, how to fix this issue?
From the 1.9 release notes (emphasis mine):
All models need to be defined inside an installed application or
declare an explicit app_label. Furthermore, it isn’t possible to
import them before their application is loaded. In particular, it
isn’t possible to import models inside the root package of an
application.
You are (indirectly) importing your models into the root package of your app, Mypack/__init__.py. If you still want to import the functions in foo.py into your root package, you need to make sure that it doesn't import the models when the module is first imported. One option is to use inline imports:
def foo():
from .models import MyModel
MyModel.objects.do_something()
If importing the functions in your root package is not a hard requirement (e.g. for backwards compatibility), I would personally just stop importing them in __init__.py and change other imports to use Mypack.foo.

Google App Engine Interactive Console: ImportError: No module named models

I have a django project running on my local server under the google app engine python SDK 1.9.7.
I want to run a query in the interactive console. I have a model called Flatpage
I put the seemingly appropriate handler in the app.yaml file, namely:
url: /admin/.*
script: google.appengine.ext.admin.application
login: admin
From the docs i understood that when the local development server is running the interactive console on the admin server would be running in the same environment as the a project. Yet:
the interactive console does not recognize any of my model classes and when i try to import models I get an error: ImportError: No module named models
I have tried adding the local path to the models.py file to the directory as well as various names like app.models, appname.models and projectname.models to no avail.
However. when I copy and paste the models file into the interactive console it works.
Would someone explain how to import the models file into the interactive console,
and, secondly, why the interactive console, which seemingly should already have the models defined when the server starts, needs to have the models defined again, anyway, thanks!
I'm not sure why you're adding that handler to use the console locally, but what I do is use the Development Console (available since last year), which by default runs on port 8000, from there (localhost:8000/console most likely) you can import modules from your project.
For example if you have models.py in the same directory as app.yaml, you should be able to do:
import models
entity = models.Flatpage()
with no problem.