2017-06-01 17 views
0

私はPythonで初心者です。あなたの助けが必要です。私はMacを持ち、Pythonを使用しています。このエラーが表示されました:NameError: name '__tablename__' is not definedPythonエラーNameError:name '__tablename__'が定義されていません

ありがとうございます!

私の全体のコードは次のとおりです。

from flask_sqlalchemy import SQLAlchemy 
    from sqlalchemy import create_engine 
    engine = create_engine('sqlite:///:memory:', echo=True) 


from sqlalchemy.ext.declarative import declarative_base 
Base = declarative_base() 



#creare de tabela 

from sqlalchemy import Column, Integer, String 
class User(Base): 
    __tablename__ = 'users' 
    id = Column(Integer, primary_key=True) 
    name = Column(String) 
    fullname = Column(String) 
    program = Column(String) 
    aka = Column(String) 
    adresa = Column(String) 
    city = Column(String) 
    state = Column(String) 
    country = Column(String) 
    postalcode = Column(String) 

def __repr__(self): 
    return "<User(name='%s', fullname='%s', program='%s', aka='%s',adresa='%s',city='%s',country='%s',postalcode='%s',)>" %(
     self.name, self.fullname, self.program, self.aka, self.adresa, self.city, self.state, self.country,self.postalcode) 

#creare de tabela 
User.__tablename__ 
table('users', MetaData(bind=None), 
    Column('id', Integer(),table=(users), primary_key=True, nullable=False), 
    Column('name', String(), table=(users)), 
    Column('fullname', String(), table=(users)), 
    Column('program', String(), table=(users)), 
    Column('aka', String(), table=(users)), 
    Column('adresa', String(), table=(users)), 
    Column('city', String(), table=(users)), 
    Column('state', String(), table=(user)), 
    Column('country', String(), table=(users)), 
    Column('postalcode', String(), table=(users), schema=None)) 

答えて

0

それはあなたのユースケースで"private variable"

[...] Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam , where classname is the current class name with leading underscore(s) stripped.

なので、あなたとあなたのvarbaileをマークし、この問題を回避するためにUser._user__tablename__

とそれにアクセスすることができますone先頭にアンダースコアを付け、2つの末尾のアンダースコアを削除します。これは、pep8の提案です。

_single_leading_underscore : weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.

+0

ありがとうTim!あなたの投稿ごとにコードを変更しましたが、同じエラーが表示されます。 –

+0

このエラーは '' 'User .__ tablename__'''行から出力されますか? – fechnert

関連する問題