gpt4 book ai didi

error-handling - Python错误NameError : name '__tablename__' is not defined

转载 作者:行者123 更新时间:2023-12-03 08:49:43 37 4
gpt4 key购买 nike

我是Python的新手,需要您的帮助。我有一台Mac,使用python3。使用SublimeText,这是我在运行代码时收到的信息==>我收到此错误:NameError: name '__tablename__' is not defined
先感谢您!!!

我的整个代码是:

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))

最佳答案

因为是"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.



在您的用例中,可以使用 User._user__tablename__访问它

为避免此问题,请在您的varbaile上标记一个下划线,并删除两个下划线。这是 pep8的建议:

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

关于error-handling - Python错误NameError : name '__tablename__' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44310441/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com