In django is there way to import all modules
ex: from project.models import *
ex: from project1.models import *
Can this be done with one statement
If you just want to do this when testing things in the shell, look into the shell_plus command provided by the django-extensions project.
This is a really neat extension, which starts a shell and automatically loads all the models in your project when you do ./manage.py shell_plus from the command line.
Related
Now, to run the bot through Django, I first use the python manage.py runserver command and then follow the link to launch a view with my bot. Can you tell me if there is an easier way to start my bot automatically when starting a Django project?
Actually, you can use a management command to run your bot with something like
python manage.py runbot
All Django context, including DB and settings, will be available
Reference to management command page:
https://simpleisbetterthancomplex.com/tutorial/2018/08/27/how-to-create-custom-django-management-commands.html
Maybe is a little late but you can do the following:
create the bot.py file in the same folder where manage.py is.
inside the bot.py make sure you import the following:
import django
import os
os.environ['DJANGO_SETTINGS_MODULE'] = '{Folder where your settings are}.settings'
django.setup()
and in order to run you just type python bot.py
I have a Django project. Everytime I deploy, I need to run a series of manage.py command (such as syncdb, south migiration, fixture update).
I am getting tired of type the command line by line and therefore I wrote a python script to do these:
import subprocess
subprocess.call(['python', 'manage.py', 'syncdb'])
#Skip the detail
subprocess.call(['python', 'manage.py', 'loaddata', 'setup/fixture.xml'])
I am wondering if there is a better way to do this?
Thanks.
You can use fabric, a Python library that allows you to script remote actions. This question has some links in the accepted answer for more information on fabric and django.
You can also call management commands directly:
from django.core.management import call_command
call_command('syncdb')
call_command('loaddata', 'setup/fixture.xml')
Save that as a normal python file and execute it from your shell or as part of your deployment scripts.
I'm using django-haystack for searching on my site.
I'm also using django multilingual model for I18n.
I import MultilingualModel in search_indexes.py
I ca run all manangement commands as long as I don't have haystack in the INSTALLED_APPS.
When haystack is in the INSTALLED_APPS and try to run syncdb or migrate (and several other management commands) I'm always getting:
django.core.exceptions.ImproperlyConfigured: ImportError haystack: cannot import name MultilingualModel
This is likely related to the hacks done in haystack.autodiscover(). This behavior is documented here: http://docs.haystacksearch.org/dev/debugging.html#import-errors-on-start-up-mentioning-handle-registrations There is a long discussion in this ticket: https://github.com/toastdriven/django-haystack/issues/84
The long and short if it is that moving haystack.autodiscover() into your urls.py can sometimes resolve this issue. Setting HAYSTACK_ENABLE_REGISTRATIONS = False when running syncdb or migrate has resolved this for me using this snippet in my settings.py:
# FIXME: This is a complete hack to get around circular imports in
# django-haystack and other apps such as django-endless-pagination
SKIP_COMMANDS = ['syncdb', 'migrate', 'schemamigration', 'datamigration']
if any([command in sys.argv for command in SKIP_COMMANDS]):
HAYSTACK_ENABLE_REGISTRATIONS = False
search_indexes.py doesn't get processed unless haystack is in INSTALLED_APPS. The problem is with the import of MultilingualModel in general. Either it's not truly installed in your environment (attempt to import it from a vanilla python shell), or you have the import wrong (it's actually in another module, for example).
Once you can successfully import MultilingualModel from a python shell, you won't have any problems.
I am using django with mod_python (I know it is deprecated -- I am just doing this for an exercise), and am getting the following error --
"Could not import project.views. Error was: No module named app.models"
I am getting the 'No module named app.models" in several other places as well. My syncdb is working fine, and if I go into the manage.py shell I can import the models fine. Why is this occurring and what do I need to change? Thank you.
You should use absolute imports everywhere. If your project is structured like so:
/project/settings.py
/project/app/models.py
/project/app/views.py
In INSTALLED_APPS you would use project.app. In app you'd import your models into views: import project.app.models, etc. Alternately you can try adjusting your PYTHONPATH so your imports work. When you run ./manage.py you are in your project folder, and Python automatically adds it to the PYTHONPATH. This doesn't happen automatically in most deployment scenarios (mod_python or other wise).
Right now, if I want to run tests from all my apps, I go:
python manage.py test app1 app2 app3
If I run:
python manage.py test
The test of all apps in INSTALLED_APPS are run, including the django ones. Is there a simple command to run the tests of all the apps that I have created?
Sadly there is no such command. Django has no way of telling which apps are "yours" versus which are someone else's.
What I would suggest is writing a new management command, call it mytest. Then create a new setting MY_INSTALLED_APPS. The mytest command will just run the test for every app in MY_INSTALLED_APPS. You'll want the mytest command to subclass django.core.management.base.AppCommand. django.core.management.call_command will also be helpful.
The only problem with this method is that you will have to constantly maintain the MY_INSTALLED_APPS setting to make sure it is correct.
You could create an management/commands/testmyapps.py for one of your app which has:
from django.core.management.base import BaseCommand, CommandError
import django.core.management.commands.test
from django.core import management
from django.conf import settings
class Command(django.core.management.commands.test.Command):
args = ''
help = 'Test all of MY_INSTALLED_APPS'
def handle(self, *args, **options):
super(Command, self).handle(*(settings.MY_INSTALLED_APPS + args), **options)
This works better in Django 1.6+: when you run python manage.py test, only your tests will run (assuming you have the default settings for TEST_RUNNER)
Use nose django test runner, it takes care of this.
Reference:
Django nose to run only project tests