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.
Related
The apps i create are working with out any need for them to be registered in settings.py of the project.
The official documentation also doesn't register its polls app in INSTALLED_APPS. https://docs.djangoproject.com/en/3.0/intro/tutorial01/
What is the use of registering apps in INSTALLED_APPS of settings.py if it works with out registering?
In python, anything you import is known to the code where it is imported, but the inverse is not possible...
Imagine I have two files
# a.py
def foo():
...
# b.py
from a import foo
def bar():
foo()
In b.py I know about foo, but a.py doesn't know about bar or even that b.py exists.
Now when you have a router, you import your views which import models, etc. Your full app can work with this. But for Django to know about your app it needs at least some reference to it. The INSTALLED_APPS in settings tells Django which apps are meant to be part of your entire Django project. By default, it includes some Django apps. Not every project needs all of them so some (or all) might be removed. Similarly, other 3rd party apps might be added.
Running Django management commands such as migrate or collectstatic will only work on apps included in INSTALLED_APPS.
P.S: Never access INSTALLED_APPS in your code, but rather use the app registry via from django.apps import apps
I have a problem with the modules in Bitnami Odoo 12 for Google Cloud.
I am studying the fundamentals of Odoo, and I was able to create my first module, all right, I appeared in the list of modules and installed it, now I am in lesson 2 where I create basic models for this module, I did everything with care, even following the steps odoo documentation, but update, restart the server and the VM several times, but in the list of models does not appear what I created.
What is happening?
Odoo installed modules get's imported, as other's already mentioned in the comment. So if you don't import your models definition. For example, let's say your model definition .py files are organized as following:
root_addon_directory/
__init__.py
models/
__init__.py
my_model.py
Then you can import your model definition using relative path import as following:
root_addon_directory __init__.py file content
from . import models
models directory __init__.py file content
from . import my_model
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
I started a Django 1.6 application locally, with the structure that it came with:
project
django_folder
wsgi.py
my_app
views.py
models.py
...
my_second_app
views.py
models.py
...
Now when I try and deploy on Heroku, it only seems to work with the Procfile in the top directory. I seem to be able to move manage.py around without problems.
My problem is that I have environmental variables set so that in my_app views.py I do the following:
from models import my_model
When I put it on Heroku, it seems that I need to specify my_app
from my_app.models import my_model
Is it possible to set an environmental variable in Heroku so I don't need the myapp in front of models? (My second app is not finished, but partly integrated. It is going to be a pain to change all the imports in both apps). When I add my_app in settings.py is this supposed to affect import paths?
You don't need environment variables, just use relative imports. So in your views.py:
from .models import MyModel
Note the leading dot making this a relative import. In this case there's no need to do anything more. This is the standard pythonic way of doing things.
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.