Web2py DAL has no attribute? - python

My web2py application returned me an error today, which is quite odd.
Traceback (most recent call last):
File "/var/www/web2py/gluon/restricted.py", line 212, in restricted
exec ccode in environment
File "/var/www/web2py/applications/1MedCloud/controllers/default.py", line 475, in <module>
File "/var/www/web2py/gluon/globals.py", line 194, in <lambda>
self._caller = lambda f: f()
File "/var/www/web2py/applications/1MedCloud/controllers/default.py", line 63, in patient_register
rows = db(db.patientaccount.email==email).select()
File "/var/www/web2py/gluon/dal.py", line 7837, in __getattr__
return ogetattr(self, key)
AttributeError: 'DAL' object has no attribute 'patientaccount'
I am using Mysql as the database, and the table 'patientaccount' does exist. There is no connection issue as I can create tables but not fetch them from the server.
I have been using the very same code to do the db thing, here is my code
db = DAL('mysql://###:$$$#^^^^^^:3306/account_info', pool_size=0)
rows = db(db.patientaccount.email==email).select()
I did not change any code in my default.py file, but accidentally deleted some files inside "database" folder in my application. But I doubt if that could result the error, since the module is fetching tables on the server rather than using local files.
Please help! Thanks in advance!

The DAL does not inspect the MySQL database to discover its tables and fields. You must define the data models explicitly. So, somewhere in your code, you must do:
db.define_table('patientaccount',
Field('email'),
...)
That will define the db.patientaccount table so the DAL knows it exists and what fields it includes.

Related

Syntax error while running mongo-connector between MongoDB and Neo4J

I am using mongo-connector to do the initial bulk_upsert operation between MongoDB and Neo4J. At some point while querying with py2neo, the InvalidSyntax exception is occurring due to which nothing is being inserted into graph database. I believe the issue lies somewhere in the DocManager during syntax translations. I am running py2neo v2.0.8 and Neo4J v2.3.1.
Here is the detailed stack trace:
Exception in thread Thread-2:
Traceback (most recent call last):
File "//anaconda/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "//anaconda/lib/python2.7/site-packages/mongo_connector/util.py", line 85, in wrapped
func(*args, **kwargs)
File "//anaconda/lib/python2.7/site-packages/mongo_connector/oplog_manager.py", line 256, in run
docman.upsert(doc, ns, timestamp)
File "//anaconda/lib/python2.7/site-packages/mongo_connector/doc_managers/neo4j_doc_manager.py", line 66, in upsert
tx.commit()
File "//anaconda/lib/python2.7/site-packages/py2neo/cypher/core.py", line 333, in commit
return self.post(self.__commit or self.__begin_commit)
File "//anaconda/lib/python2.7/site-packages/py2neo/cypher/core.py", line 288, in post
raise self.error_class.hydrate(error)
InvalidSyntax: Invalid input '{': expected whitespace, comment or a label name (line 1, column 20 (offset: 19))
"MERGE (d:Document: { _id: {parameters}._id})"
What could be happening here?
Thanks for reporting this.
Neo4j Doc Manager uses a key naming convention of xxx_id to identify relationships, where the value of a property with key xxx_id is assumed to be an id referencing a document in collection xxx. This convention allows us to define relationships from the document data model. I'm assuming that the error here is caused by Neo4j Doc Manager treating the nested document's _id field as a relationship, but not checking for a null collection name (since nothing appears before "_id" in the key).
This is a bug and we'll add a check for this to avoid the Cypher syntax error. Those interested can track the issue here: https://github.com/neo4j-contrib/neo4j_doc_manager/issues/56

AttributeError: 'NoneType' object has no attribute '_default_manager' when running lettuce with django rest framework

I am trying to use lettuce to implement BDD for creating an rest framework api but when I execute the command python manage.py harvest I get following error
/rest_framework/serializers.py", line 818, in get_related_field
'queryset': related_model._default_manager,
AttributeError: 'NoneType' object has no attribute '_default_manager'
Detailed stack trace is:
(envSamplePayApp)root#ubuntu:/pythonWorkspace/samplePayProject# python manage.py harvest
Django's builtin server is running at 0.0.0.0:8000
Traceback (most recent call last):
File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/lettuce/django/management/commands/harvest.py", line 167, in handle
result = runner.run()
File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/lettuce/__init__.py", line 137, in run
self.loader.find_and_load_step_definitions()
File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/lettuce/fs.py", line 49, in find_and_load_step_definitions
module = __import__(to_load)
File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/rest_framework/tests/test_serializer_import.py", line 4, in <module>
from rest_framework.tests.accounts.serializers import AccountSerializer
File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/rest_framework/tests/accounts/serializers.py", line 6, in <module>
class AccountSerializer(serializers.ModelSerializer):
File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/rest_framework/tests/accounts/serializers.py", line 8, in AccountSerializer
admins = UserSerializer(many=True)
File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 196, in __init__
self.fields = self.get_fields()
File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 232, in get_fields
default_fields = self.get_default_fields()
File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 716, in get_default_fields
field = self.get_related_field(model_field, related_model, to_many)
File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages
All the code can be found at downloadcode.
Note when I run the api from browser it works fine.
What is causing the AttributeError: 'NoneType' object has no attribute '_default_manager'?
I ran into this error just now. The cause of my error was due to the fact that I had created a ManyToManyField from Model X of App A to Model Y of App B. When I tried to create schema migration via python manage.py schemamigration --auto app_a, it raised this error because I had yet to:
include 'app_b' in the INSTALLED_APPS tuple of settings.py
not having created the initial schema migration for app_b (which would not be possible with adding app_b to INSTALLED_APPS).
Effectively, I linked to an model that was not known at the time. Hopefully this can help someone in the future.
EDIT: Actually, on further inspection, I realize now the reason that this error is being raised so ambiguously is because of django-debug-toolbar. Apparently DjangoDebugToolbar is causing some issues around errors propagating properly. Disabling it in INSTALLED_APPS allowed the errors to become apparent.

How to fill table using mysql connector in Python 3

I have data in a text file which I need to upload into a table. My script in python 3 and uses mysql.connector (https://launchpad.net/myconnpy) to connect to DB and execute commands. I have been able successfully use mysql.connector in past without any problems but I am facing problem in using the command that uploads file to a table. My code is as follows:
def TableUpload(con2):
cur = con2.cursor()##Connect to destination server with table
res_file = 'extend2'
cur.execute("TRUNCATE TABLE data.results")## Clear table before writing
cur.execute("LOAD DATA LOCAL INFILE './extend2' INTO TABLE data.results FIELDS TERMINATED BY ','")
The code clears the table and than try to upload data from text file to table. It successfully clears the table but generated following error while filling table:
Traceback (most recent call last):
File "cl3.py", line 575, in <module>
TableUpload(con2)
File "cl3.py", line 547, in TableUpload
cur.execute("LOAD DATA LOCAL INFILE './extend2' INTO TABLE kakrana_data.mir_page_results FIELDS TERMINATED BY ','")
File "/usr/local/lib/python3.2/site-packages/mysql/connector/cursor.py", line 333, in execute
res = self.db().protocol.cmd_query(stmt)
File "/usr/local/lib/python3.2/site-packages/mysql/connector/protocol.py", line 137, in deco
return func(*args, **kwargs)
File "/usr/local/lib/python3.2/site-packages/mysql/connector/protocol.py", line 495, in cmd_query
return self.handle_cmd_result(self.conn.recv())
File "/usr/local/lib/python3.2/site-packages/mysql/connector/connection.py", line 180, in recv_plain
errors.raise_error(buf)
File "/usr/local/lib/python3.2/site-packages/mysql/connector/errors.py", line 84, in raise_error
raise get_mysql_exception(errno,errmsg)
mysql.connector.errors.NotSupportedError: 1148: The used command is not allowed with this MySQL version
When I use the command for uploading file directly from terminal than it works well. It is just that command is not working from script. The error says that command is not allowed with mysql version though it works from terminal. Please suggest what mistake I am making or alternative way to achieve data upload to a table from local file.

Testing Django database manipulation scripts with test database

I am attempting to setup the django testing environment to test a script we use to load data into our data visualization web app. It is "external" to the actual django app, so it doesn't seem appropriate or clean to use the manage.py test facility.
I have followed the instructions found in the django docs to write my own test module, but when I reach a statement that actually tries to access the database I get an error saying that a table does not exist.
Is there something else I need to do to make sure that the test database is created with all of the needed tables?
For reference, here is the code from my test case:
import os
import sys
sys.path.append(os.curdir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'visualization_app.settings'
from django.utils import unittest
from django.test.utils import setup_test_environment
from topic_modeling import settings
# The module to be tested
import analysis_import
setup_test_environment()
class CreateAnalysisTestCase(unittest.TestCase):
def test_analysis_creation(self):
self.assertTrue(analysis_import.create_analysis('a_name', 'a description'))
if __name__ == '__main__':
unittest.main()
The error I get looks like this:
ERROR: test_analysis_creation (__main__.CreateAnalysisTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "import_scripts/tests.py", line 20, in test_analysis_creation
self.assertTrue(analysis_import.create_analysis('a_name', 'a description'))
File "/home/dan/programmingProjects/topical_guide/import_scripts/analysis_import.py", line 117, in create_analysis
Analysis.objects.get(name=name, dataset=dataset)
File "/usr/local/lib/python2.7/dist-packages/Django-1.3-py2.7.egg/django/db/models/manager.py", line 132, in get
return self.get_query_set().get(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.3-py2.7.egg/django/db/models/query.py", line 344, in get
num = len(clone)
File "/usr/local/lib/python2.7/dist-packages/Django-1.3-py2.7.egg/django/db/models/query.py", line 82, in __len__
self._result_cache = list(self.iterator())
File "/usr/local/lib/python2.7/dist-packages/Django-1.3-py2.7.egg/django/db/models/query.py", line 273, in iterator
for row in compiler.results_iter():
File "/usr/local/lib/python2.7/dist-packages/Django-1.3-py2.7.egg/django/db/models/sql/compiler.py", line 680, in results_iter
for rows in self.execute_sql(MULTI):
File "/usr/local/lib/python2.7/dist-packages/Django-1.3-py2.7.egg/django/db/models/sql/compiler.py", line 735, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/Django-1.3-py2.7.egg/django/db/backends/util.py", line 34, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/Django-1.3-py2.7.egg/django/db/backends/sqlite3/base.py", line 234, in execute
return Database.Cursor.execute(self, query, params)
DatabaseError: no such table: visualize_analysis
Strictly speaking, test databases are intended to be created, used to run unit tests and then destroyed. They're not really a place to "test" a data loading script. Why not just use your standard dev database? There shouldn't be anything there that you can't get back, have a backup for, etc., and it's pretty much designed for this sort of destructive behavior.
As far as I can see, the module you are testing still uses django's ORM system to read the database, so there is anything wrong about using manage.py to run your test. You can use
./manage.py test analysis_import
to only run test codes which reside in analysis_import module. In this way, you can still run only the tests for this module, but avoid the trouble of writing your own test module.
Then let us know if you still have the same problem.

changing django default model settings

I'm just starting with the django creating your own app tutorial (creating a Poll) I'm deviating slightly as I'm wanting to create an app using my own database model that already exists.
And in the tutorial it says
Table names are automatically
generated by combining the name of
the app (polls) and the lowercase
name of the model -- poll and choice.
(You can override this behavior.)
Primary keys (IDs) are added
automatically. (You can override
this, too.)
By convention, Django appends
"_id" to the foreign key field
name. Yes, you can override this,
as well.
But I can't see where it mentions how you can override this behaviour? I've defined my model as so
from django.db import models
# Create your models here.
class Channels(models.Model):
channelid = models.IntegerField()
channelid.primary_key = True
channelname = models.CharField(max_length=50)
Now when I go in to the shell this is what I get
>>> from tvlistings.models import *
>>> Channels.objects.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python26\lib\site-packages\django\db\models\query.py", line 67, in __
repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "C:\Python26\lib\site-packages\django\db\models\query.py", line 82, in __
len__
self._result_cache.extend(list(self._iter))
File "C:\Python26\lib\site-packages\django\db\models\query.py", line 271, in i
terator
for row in compiler.results_iter():
File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 67
7, in results_iter
for rows in self.execute_sql(MULTI):
File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 73
2, in execute_sql
cursor.execute(sql, params)
File "C:\Python26\lib\site-packages\django\db\backends\util.py", line 15, in e
xecute
return self.cursor.execute(sql, params)
File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 86
, in execute
return self.cursor.execute(query, args)
File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "C:\Python26\lib\site-packages\MySQLdb\connections.py", line 35, in defau
lterrorhandler
raise errorclass, errorvalue
DatabaseError: (1146, "Table 'tvlistings.tvlistings_channels' doesn't exist")
Obviously it can't find the table tvlistings_channels as it's actually called channels. So how do I change the default?
You can override Model behavior in Django largely through the use of an inner Meta class
db_table allows you to rename the table name
specifying another field as the primary key will have it use that rather than a surrogate key (not in the Meta class, just in the model itself)
You should try and work your way all through the tutorial before you try and customise things. All these things are covered in the actual documentation, but it's best to have a basic understanding of things first before diving into that.
FWIW, here are the docs on defining your own primary key and specifying a table name. But really, do the tutorial as written first.

Categories

Resources