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

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.

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

What's the best way to make a new migration in a standalone Django app?

I have a Django app which was spun out a while ago from the project it was originally built for, made into a separate standalone app, and put on PyPI (https://pypi.python.org/pypi/mysociety-django-images). The app is built expecting to be run with Django 1.7+.
I'd now like to make a change to one of the models there - just changing a max_length on a field. I can't see anything in the documentation about how to make a new migration for it? Do I need to make an example project and use that, or is there a better way?
You can do this by making a script like:
#!/path/to/your python
import sys
import django
from django.conf import settings
from django.core.management import call_command
settings.configure(
DEBUG=True,
INSTALLED_APPS=(
'django.contrib.contenttypes',
'yourAppName',
),
)
django.setup()
call_command('makemigrations', 'yourAppName')
(this is also how we go about testing our standalone apps).
I don't know which is the better practice between this and create an example project (this appears lighter to do).

Heroku application structure for Django

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.

Django Import Model Error

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.

django automate syncdb

I'm working on a project that includes a django server, and also a setup module.
The user will be configuring their system to run my program, which includes a django webserver element along with other items. I'm working on a setup module that assists the user in getting all of the settings correct and sets up all of the appropriate files. One of the things that I'd like to be during the setup process is essentially a "manage.py syncdb" command that creates an appropriate SQLite file and table from nothing.
I could grab the code found in manage.py and directly stick it into my setup module appropriately, but I'm not sure if there's a better approach that I'm missing - along the lines of two lines consisting of:
import django.something
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.something.syncdb()
Or something of the sort. Am I just missing something here?
This should do it:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core import management
management.call_command('syncdb', interactive=False)
You can also do
import os
import settings
from django.core.management.commands import syncdb
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
syncdb.Command().execute(noinput=True)