Here is the beginning of my code:
import os
from flask import Flask, render_template, session, redirect, url_for, flash
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
from flask_sqlalchemy import SQLAlchemy
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
app.config['SQLALCHEMY_DATABASE_URI'] =\
'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
bootstrap = Bootstrap(app)
moment = Moment(app)
db = SQLAlchemy(app)
a=db.Model
class Role(db.Model):
__tablename__ = 'roles'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), unique=True)
users = db.relationship('User', backref='role')
def __repr__(self):
return '<Role %r>' % self.name
As i do not see an init method in Role, i suppose it is in parent class db.Model. So i wanted to see how this class is defined. I created the object named 'a' in the upper script and in a python terminal run:
In [11]: inspect.getmodule(a)
Out[11]: <module 'sqlalchemy.ext.declarative.api' from 'c:\\users\\laurent\\google drive\\python\\venv\\lib\\site-packages\\sqlalchemy\\ext\\declarative\\api.py'>
In [12]: inspect.getsource(a)
...
C:\Python35\lib\inspect.py in findsource(object)
785 return lines, candidates[0][1]
786 else:
--> 787 raise OSError('could not find class definition')
788
789 if ismethod(object):
OSError: could not find class definition
Why do i get this error ? The class seemed to come from a python script (not C code). Moreover in bonus if someone could provide me the __init__method of the parent class it would be wonderful!
Thanks (i am autodicact in code so any advices to inspect/debug code would be interesting, perhaps an other dedicated question...).