Custom python database logger, having circular import - python

I am trying to create my own log handler to log to db models, which extends logging.Handler
import logging
from logging import Handler
from logger.models import SearchLog
class DBHandler(Handler,object):
model = None
def __init__(self, model):
super(DBHandler, self).__init__()
mod = __import__(model)
components = name.split('.')
for comp in components[1:]:
mod = getattr(mod, comp)
self.model = mod
def emit(self,record):
log_entry = self.model(level=record.levelname, message=record.msg)
log_entry.save()
and this is the log config:
'db_search_log':{
'level': 'INFO',
'class': 'db_logger.handlers.DBHandler',
'model': 'db_logger.models.SearchLog',
'formatter': 'verbose',
}
however I am getting the follow error, see stacktrace:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 252, in fetch_command
app_name = get_commands()[subcommand]
File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 101, in get_commands
apps = settings.INSTALLED_APPS
File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/conf/__init__.py", line 135, in __init__
logging_config_func(self.LOGGING)
File "/usr/lib/python2.7/logging/config.py", line 777, in dictConfig
dictConfigClass(config).configure()
File "/usr/lib/python2.7/logging/config.py", line 575, in configure
'%r: %s' % (name, e))
ValueError: Unable to configure handler 'db_search_log': Unable to configure handler 'db_search_log': 'module' object has no attribute 'handlers'
▾
db_logger/
__init__.py
__init__.pyc
handlers.py
handlers.pyc
log_handlers.pyc
models.py
models.pyc
router.py
router.pyc
tests.py
views.py
Thanks to #istruble pointed out that that is due to circular imports of settings, how can I avoid it and still log to the database models?

I just came up with another actually more canonical way of implementing it using delayed imports, my original problem was trying to import the model inside init function:
from logging import Handler
class DBHandler(Handler,object):
model_name = None
def __init__(self, model=""):
super(DBHandler,self).__init__()
self.model_name = model
def emit(self,record):
# instantiate the model
try:
model = self.get_model(self.model_name)
except:
from logger.models import GeneralLog as model
log_entry = model(level=record.levelname, message=self.format(record))
log_entry.save()
def get_model(self, name):
names = name.split('.')
mod = __import__('.'.join(names[:-1]), fromlist=names[-1:])
return getattr(mod, names[-1])

I got a work around and I admit it looks like a hack, which uses the model injection at actual logging point like this
from logging import Handler
class DBHandler(Handler,object):
def __init__(self):
super(DBHandler, self).__init__()
def emit(self,record):
model = record.model
log_entry = model(level=record.levelname, message=record.msg)
log_entry.save()
and you log it to the correct model by doing this:
import logging
import logger.models.TheModel
logger = logging.getLogger(__name__)
logger.info(123, extra={'model':TheModel})

Related

Django Exception : AppRegistryNotReady("Models aren't loaded yet.")

I found a lot of content on the AppRegistryNotReady Exception, but none of them seem defenitive. I just wanted my 2 cents of info on the topic.
My django project was working fine. I created a new app, and created the following model. No view, no urls nothing. Just a model.
from __future__ import unicode_literals
from django.db import models
# Create your models here.
from django.conf import settings
from django.core.exceptions import ValidationError
from django.contrib.auth import get_user_model
User = get_user_model()
class File(models.Model):
path = models.TextField() #The path does not include MEDIA_ROOT, obviously
filename = models.CharField(max_length=500)
# file = models.FileField(upload_to=upload_to)
file = models.FileField(upload_to=path+filename)
user = models.ForeignKey(settings.AUTH_USER_MODEL, models.PROTECT) #Protects User from being deleted when there are files left
def clean(self):
#Check if path has a trailing '/'
if self.path[-1]!='/':
self.path = self.path+"/"
if self.filename[0]=='/':
self.filename = self.filename[1:]
#Get the full path
username = self.user.__dict__[User.USERNAME_FIELD] #Need to do this the roundabout way to make sure that this works with CUSTOM USER MODELS. Else, we could have simply went for self.user.username
self.path = "tau/"+username+"/"+self.path
def save(self, *args, **kwargs):
self.full_clean()
return super(File, self).save(*args, **kwargs)
def __str__(self):
if path[-1]=='/':
text = "\n"+str(path)+str(filename)
else:
text = "\n"+str(path)+"/"+str(filename)
return text
Then I tried to makemigrations on the model. And ended up with the following error.
(test) ~/Workspace/WebDevelopment/Django/test/stud$python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/raghuram/Workspace/WebDevelopment/Django/test/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/raghuram/Workspace/WebDevelopment/Django/test/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 341, in execute
django.setup()
File "/home/raghuram/Workspace/WebDevelopment/Django/test/local/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/raghuram/Workspace/WebDevelopment/Django/test/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home/raghuram/Workspace/WebDevelopment/Django/test/local/lib/python2.7/site-packages/django/apps/config.py", line 199, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/raghuram/Workspace/WebDevelopment/Django/test/stud/tau/models.py", line 10, in <module>
User = get_user_model()
File "/home/raghuram/Workspace/WebDevelopment/Django/test/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 163, in get_user_model
return django_apps.get_model(settings.AUTH_USER_MODEL)
File "/home/raghuram/Workspace/WebDevelopment/Django/test/local/lib/python2.7/site-packages/django/apps/registry.py", line 192, in get_model
self.check_models_ready()
File "/home/raghuram/Workspace/WebDevelopment/Django/test/local/lib/python2.7/site-packages/django/apps/registry.py", line 131, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
Just for the sake of completing the test, I changed my model to this,
class File(models.Model):
file = models.FileField()
And that stopped the exception. So my guess is that the Exception was being raised by this.
from django.contrib.auth import get_user_model
User = get_user_model()
But I need to use that, since Im working with custom User Model. Any idea on how I can make it happen?
AbstractBaseUser provides a get_username() method which you can use instead. It practically does the same as what you're doing: return getattr(self, self.USERNAME_FIELD).
class File(models.Model):
...
def clean(self):
#Check if path has a trailing '/'
if self.path[-1]!='/':
self.path = self.path+"/"
if self.filename[0]=='/':
self.filename = self.filename[1:]
#Get the full path
username = self.user.get_username()
self.path = "tau/"+username+"/"+self.path
The reason your original method failed is because get_user_model() is executed when the module is first imported, at which time the app registry is not fully initialized. If you need to use get_user_model() in a models.py file, you should call it within a function or method, not at the module level:
class File(models.Model):
...
def clean(self):
User = get_user_model()

Flask-SQLAlchemy unittests failing InvalidRequestError: Table '{table_name}' is already defined for this MetaData instance

I'm building an API service with Flask, SQLAlchemy and more recently integrating the Flask-SQLAlchemy extension. While I can run the app standalone and make API calls successfully, I am hitting a problem attempting to run unittests. I believe the issue is with importing the db.Model types more than once.
The exception is this:
Error
Traceback (most recent call last):
File "/Users/james/.pyenv/versions/2.7.10/lib/python2.7/unittest/case.py", line 322, in run
self.setUp()
File "/Users/james/Documents/workspace/trustmile-backend/trustmile/tests/test_users.py", line 28, in setUp
from trustmile.app.users.model import User, ConsumerUser, CourierUser, AuthSession, Location, UserAddress, db
File "/Users/james/Documents/workspace/trustmile-backend/trustmile/app/users/model.py", line 23, in <module>
class User(db.Model, UniqueMixin, TableColumnsBase, References):
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 536, in __init__
DeclarativeMeta.__init__(self, name, bases, d)
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py", line 55, in __init__
_as_declarative(cls, classname, cls.__dict__)
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 88, in _as_declarative
_MapperConfig.setup_mapping(cls, classname, dict_)
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 103, in setup_mapping
cfg_cls(cls_, classname, dict_)
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 131, in __init__
self._setup_table()
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 394, in _setup_table
**table_kw)
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 398, in __new__
"existing Table object." % key)
InvalidRequestError: Table 'tmuser' is already defined for this MetaData instance. Specify 'extend_existing=True' to redefine options and columns on an existing Table object.
My init.py in /app/ (the top level) is like this:
__author__ = 'james'
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
import config
from app.messaging import EmailHandler
app = Flask(__name__, static_folder='api/static', static_url_path='/static')
app.config.from_object(config)
db = SQLAlchemy(app)
EmailHandler.setup(config.EMAIL_API_KEY_DEV)
from app.api.consumer_v1 import bp as blueprint
app.register_blueprint(blueprint, url_prefix = '/consumer/v1')
app.test_request_context()
Running that with:
from app import app
app.run(debug=True, host='0.0.0.0', port=5001)
Works great.
The beginning of my test_users.py looks like this:
__author__ = 'james'
from trustmile.app.exc import InvalidEmailException
from trustmile.app.exc import InsecurePasswordException
from nose.tools import assert_true, raises
from trustmile.app.users.model import User, ConsumerUser, CourierUser, AuthSession, Location, UserAddress, db
from . import TransactionalTest
email_address = 'james#cloudadvantage.com.au'
test_password = 'mypassword'
class UserTest(TransactionalTest):
#classmethod
def setUpClass(cls):
super(UserTest, cls).setUpClass()
def setUp(self):
super(UserTest, self).setUp()
def test_create_user(self):
user = User()
db.session.add(user)
It must be fairly common way to do unit testing and yes I'm sharing the SQLAlchemy object between modules (which I'm sure is bad practice). I'm running it with nosetests and the error occurs as the code is initialised. Sorry for the lengthy question, hopefully someone can help me!
__table_args__ = {'extend_existing': True}
right below __tablename__

testing Django with standard Unittest: 'DatabaseWrapper' object has no attribute 'Database'

I am trying to test my Django test files separately, one by one, as ./manage.py test freezes for 5secs after each run due to heavy apps.
This is my test (not even a test yet though, just playing with requests):
if __name__ == "__main__":
import unittest
# manually get all the django stuff into memory if file is called directly
import os,sys
TEST_ROOT = os.path.realpath(os.path.dirname(__file__))
PROJ_AND_TEST_ROOT = os.path.dirname(os.path.dirname(TEST_ROOT))
PROJECT_ROOT = os.path.join(PROJ_AND_TEST_ROOT, 'the_game')
sys.path.append(PROJECT_ROOT)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "the_game.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
from django.test import TestCase, RequestFactory
from django.contrib.auth.models import User
from interface.views import bets
class ViewTests(TestCase):
def test_bets_view(self):
request_factory=RequestFactory()
request=request_factory.get('/')
user = User.objects.create_user('testname','test#na.me','testname')
request.user=user
response=bets(request)
print response
if __name__ == "__main__":
unittest.main()
However, running this I get the following:
======================================================================
ERROR: test_bets_view (__main__.ViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/test/testcases.py", line 182, in __call__
self._pre_setup()
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/test/testcases.py", line 754, in _pre_setup
self._fixture_setup()
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/test/testcases.py", line 887, in _fixture_setup
if not connections_support_transactions():
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/test/testcases.py", line 874, in connections_support_transactions
for conn in connections.all())
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/test/testcases.py", line 874, in <genexpr>
for conn in connections.all())
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/utils/functional.py", line 55, in __get__
res = instance.__dict__[self.func.__name__] = self.func(instance)
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/db/backends/__init__.py", line 782, in supports_transactions
self.connection.leave_transaction_management()
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/db/backends/__init__.py", line 338, in leave_transaction_management
if managed == self.get_autocommit():
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/db/backends/__init__.py", line 345, in get_autocommit
self.ensure_connection()
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
self.connect()
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/db/utils.py", line 86, in __exit__
db_exc_type = getattr(self.wrapper.Database, dj_exc_type.__name__)
AttributeError: 'DatabaseWrapper' object has no attribute 'Database'
The project itself works perfectly without errors (./manage.py runserver), as do django tests (./manage.py test ../tests).
How can I fix this?
p.s. This is not a duplicate of this question. Its author had problems with standard Django testing, while it works fine for my project. My trouble is with third-party testing.
Unfortunately, what you're doing is not quite enough to initialize django's settings system. You can try this:
from django.core.management import setup_environ
from mysite import settings
setup_environ(settings)
Or you could look into writing a custom managment command (which will have the settings system initialized already) which calls the test runner.

How to import django models in scrapy pipelines.py file

I'm trying to import models of one django application in my pipelines.py to save data using django orm. I created a scrapy project scrapy_project in the first involved django application "app1" (is it a good choice by the way?).
I added these lines to my scrapy settings file:
def setup_django_env(path):
import imp, os
from django.core.management import setup_environ
f, filename, desc = imp.find_module('settings', [path])
project = imp.load_module('settings', f, filename, desc)
setup_environ(project)
current_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
setup_django_env(os.path.join(current_dir, '../../d_project1'))
When I try to import models of my django application app1 I get this error message:
Traceback (most recent call last):
File "/usr/local/bin/scrapy", line 4, in <module>
execute()
File "/usr/local/lib/python2.7/dist-packages/scrapy/cmdline.py", line 122, in execute
_run_print_help(parser, _run_command, cmd, args, opts)
File "/usr/local/lib/python2.7/dist-packages/scrapy/cmdline.py", line 76, in _run_print_help
func(*a, **kw)
File "/usr/local/lib/python2.7/dist-packages/scrapy/cmdline.py", line 129, in _run_command
cmd.run(args, opts)
File "/usr/local/lib/python2.7/dist-packages/scrapy/commands/crawl.py", line 43, in run
spider = self.crawler.spiders.create(spname, **opts.spargs)
File "/usr/local/lib/python2.7/dist-packages/scrapy/command.py", line 33, in crawler
self._crawler.configure()
File "/usr/local/lib/python2.7/dist-packages/scrapy/crawler.py", line 41, in configure
self.engine = ExecutionEngine(self, self._spider_closed)
File "/usr/local/lib/python2.7/dist-packages/scrapy/core/engine.py", line 63, in __init__
self.scraper = Scraper(crawler)
File "/usr/local/lib/python2.7/dist-packages/scrapy/core/scraper.py", line 66, in __init__
self.itemproc = itemproc_cls.from_crawler(crawler)
File "/usr/local/lib/python2.7/dist-packages/scrapy/middleware.py", line 50, in from_crawler
return cls.from_settings(crawler.settings, crawler)
File "/usr/local/lib/python2.7/dist-packages/scrapy/middleware.py", line 29, in from_settings
mwcls = load_object(clspath)
File "/usr/local/lib/python2.7/dist-packages/scrapy/utils/misc.py", line 39, in load_object
raise ImportError, "Error loading object '%s': %s" % (path, e)
ImportError: Error loading object 'scrapy_project.pipelines.storage.storage': No module named dydict.models
Why cannot scrapy access django application models (given that app1 in the installed_app ) ?
In the pipelines you don't import django models, you use scrapy models bounded to a django model.
You have to add Django Settings at scrapy settings, not after.
To use django models in scrapy project you have to use django_Item
https://github.com/scrapy-plugins/scrapy-djangoitem (import to your pythonpath)
My recommended file structure is:
Projects
|-DjangoScrapy
|-DjangoProject
| |-Djangoproject
| |-DjangoAPP
|-ScrapyProject
|-ScrapyProject
|-Spiders
Then in your scrapy project you hace to add pythonpath ull path to the django project:
**# Setting up django's project full path.**
import sys
sys.path.insert(0, '/home/PycharmProject/scrap/DjangoProject')
# Setting up django's settings module name.
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'DjangoProject.settings'
Then in your items.py you cand bound your Django models to scrapy models:
from DjangoProject.models import Person, Job
from scrapy_djangoitem import DjangoItem
class Person(DjangoItem):
django_model = Person
class Job(DjangoItem):
django_model = Job
Then u can use the .save() method in pipelines after yeld of an object:
spider.py
from scrapy.spider import BaseSpider
from mybot.items import PersonItem
class ExampleSpider(BaseSpider):
name = "example"
allowed_domains = ["dmoz.org"]
start_urls = ['http://www.dmoz.org/World/Espa%C3%B1ol/Artes/Artesan%C3%ADa/']
def parse(self, response):
# do stuff
return PersonItem(name='zartch')
pipelines.py
from myapp.models import Person
class MybotPipeline(object):
def process_item(self, item, spider):
obj = Person.objects.get_or_create(name=item['name'])
return obj
I have a repository with the minimal code working: (you just have to set the path of your django project in scrapy settings)
https://github.com/Zartch/Scrapy-Django-Minimal
in:
https://github.com/Zartch/Scrapy-Django-Minimal/blob/master/mybot/mybot/settings.py
You have to change my Django Project path to your DjangoProject path:
sys.path.insert(0, '/home/zartch/PycharmProjects/Scrapy-Django-Minimal/myweb')
Try:
from .. models import MyModel
OR
from ... models import MyModel
Every dot represent the location

How to add data to a relationship with multiple foreign keys in Django?

How to add data to a relationship with multiple foreign keys in Django?
I'm building a simulation written in python and would like to use django's orm to store (intermediate and final) results in a database. Therefore I am not concerned with urls and views.
The problem I am having is the instantiation of an object with multiple ForeinKeys. This object is supposed to represent the relationship between an agent and a time step.
Here is the code:
I'm using the following models
from django.db import models
import random
from django.contrib.admin.util import related_name
# Create your models here.
class Agent(models.Model):
agent = int
def __init__(self, agent, *args, **kwargs):
models.Model.__init__(self, *args, **kwargs)
self.agent = agent
def __str__(self):
return str(self.agent)
class Step(models.Model):
step = int
def __init__(self, step, *args, **kwargs):
models.Model.__init__(self, *args, **kwargs)
self.step = step
def __str__(self):
return str(self.step)
class StepAgentData(models.Model):
def __init__(self, step, agent, *args, **kwargs):
models.Model.__init__(self, *args, **kwargs)
self.step = step #This does not work
self.agent = agent
step = models.ForeignKey(Step, related_name='time_step')
agent = models.ForeignKey(Agent, related_name='associated_agent')
data = float
def __unicode__(self):
return str("Step %s \t Agent %s ", (self.step,self.agent))
Running the following script
from DBStructureTest.App.models import Step, Agent, StepAgentData
if __name__ == '__main__':
s = Step(1)
s.save()
a = Agent(2)
a.save()
sad = StepAgentData(s,a)
sad.save()
print "finished"
results in the following error message when self.step = step is executed in the constructor of the StepAgentData (see comment in the code of the StepAgentData model)
> File
> "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py",
> line 367, in __set__
> val = getattr(value, self.field.rel.get_related_field().attname)
> File
> "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py",
> line 773, in get_related_field
> data = self.to._meta.get_field_by_name(self.field_name) File
> "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py",
> line 307, in get_field_by_name
> cache = self.init_name_map() File
> "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py",
> line 337, in init_name_map
> for f, model in self.get_all_related_m2m_objects_with_model():
> File
> "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py",
> line 414, in get_all_related_m2m_objects_with_model
> cache = self._fill_related_many_to_many_cache() File
> "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py",
> line 428, in _fill_related_many_to_many_cache
> for klass in get_models(): File
> "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py",
> line 167, in get_models
> self._populate() File
> "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py",
> line 61, in _populate
> self.load_app(app_name, True) File
> "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py",
> line 76, in load_app
> app_module = import_module(app_name) File
> "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py",
> line 35, in import_module
> __import__(name) ImportError: No module named App
The folder structure is the following:
|-DBStructureTest
|-App
|-__init__.py
|-Main.py
|-models.py
|-test.py
|-views.py
|-__init__.py
|-manage.py
|-settings.py
|-urls.py
What am I doing wrong? Help is very much appreciated.
EDIT:
In the /App directory I get the following after doing 'import models'
`
import models
Traceback (most recent call last):
File "", line 1, in
File "models.py", line 1, in
from django.db import models
File "/usr/local/lib/python2.7/dist-packages/django/db/init.py", line 14, in
if not settings.DATABASES:
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 276, in getattr
self._setup()
File "/usr/local/lib/python2.7/dist-packages/django/conf/init.py", line 40, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
'
these are two different problems. the original one is because it seems that you are running your script from DBStructureTest directory, so it is trying to find DBStructureTest subdirectory under it. the second one is self-explanatory, you need to set the env var or run "manage.py shell"
BTW, I do not see any database fields in your models. Do the corresponding SQL tables contain anything?
First, you might want to use a ManyToMany-relationship for the the StepAgent-Class. You can also have extra fields on many-to-many relationships and it will be better design.
For your error you have to export DJANGO_SETTINGS_MODULE=settings or however the settingsmodule can be reached from the current PYTHONPATH, otherwise django does not know where to find and how to set up your settings.

Categories

Resources