从数据库中使用SQLalchemy提取两列数据。

huangapple go评论82阅读模式
英文:

Fetch two columns from db using SQLalchemy

问题

I am learning python and trying to create a drop-down in my form with the data from another table. here is my code

Models.py

class empmasterModel(db.Model):
    __tablename__ = "empmaster"
     
    Empnum = db.Column(db.Integer, primary_key=True)
    Employee_Number = db.Column(db.Integer(),primary_key=True,unique = True)
    Employee_Name = db.Column(db.String())
    Employee_Type = db.Column(db.String())
    Coreid = db.Column(db.String())
    EmailId = db.Column(db.String())
    
    def __init__(self, Employee_Number,Employee_Name,Employee_Type,Coreid,EmailId):
        self.Employee_Number = Employee_Number
        self.Employee_Name = Employee_Name
        self.Employee_Type = Employee_Type
        self.Coreid = Coreid
        self.EmailId = EmailId
      
    def __repr__(self):
        return f"{self.Employee_Number}:{self.Employee_Name}:{self.Employee_Type}:{self.Coreid}:{self.EmailId}"

Above is my models.py code with class empmaster from which DB table I need the employee number and employee name.

app.py

@app.route('/component/add', methods=['GET', 'POST'])
def componentadd():
    error_msg = ''
    success_msg = ''
    if request.method == 'GET':
        empmaster_data = empmasterModel.query.all()
        print(empmaster_data , "dbbbbbbbbbbbbb")
        return render_template('component/add.html', empmaster_data=empmaster_data)

Above is the app.py code where I am trying to fetch data from the empmaster table. But here I am getting the whole table data but not two-column data. I tried two column names in brackets after the filter too but it did not work. I searched for many solutions but was not getting desired results. Can somebody help me?

英文:

I am learning python and trying to create a drop-down in my form with the data from another table. here is my code

Models.py

class empmasterModel(db.Model):
__tablename__ = "empmaster"
 
Empnum = db.Column(db.Integer, primary_key=True)
Employee_Number = db.Column(db.Integer(),primary_key=True,unique = True)
Employee_Name = db.Column(db.String())
Employee_Type = db.Column(db.String())
Coreid = db.Column(db.String())
EmailId = db.Column(db.String())

def __init__(self, Employee_Number,Employee_Name,Employee_Type,Coreid,EmailId):
    self.Employee_Number = Employee_Number
    self.Employee_Name = Employee_Name
    self.Employee_Type = Employee_Type
    self.Coreid = Coreid
    self.EmailId = EmailId
  
def __repr__(self):
    return f"{self.Employee_Number}:{self.Employee_Name}:{self.Employee_Type}:{self.Coreid}:{self.EmailId}"		

Above is my models.py code with class empmaster from which DB table I need the employee number and employee name.

app.py

@app.route('/component/add', methods=['GET', 'POST'])
def componentadd():
    error_msg = ''
    success_msg = ''
    if request.method == 'GET':
        empmaster_data = empmasterModel.query.all()
        print(empmaster_data , "dbbbbbbbbbbbbb")
        return render_template('component/add.html', empmaster_data=empmaster_data)

Above is the app.py code where I am trying to fetch data from empmaster table. But here I am getting whole table data but not two-column data. I tried two column names in brackets after the filter too but it did not work. I searched for many solutions but was not getting desired results. Can somebody help me?

答案1

得分: 1

如果您使用会话管理,您可以像这样操作:

session.query(
    empmasterModel.Employee_Number.label("Employee_Number"),
    empmasterModel.Employee_Name.label("Employee_Name")
).all()

如果您使用Flask而没有会话管理,您可以从SQLAlchemy实例获取会话。

db = SQLAlchemy() # 通常在您的app.py中

然后:

db.session.query(
    empmasterModel.Employee_Number.label("Employee_Number"),
    empmasterModel.Employee_Name.label("Employee_Name")
).all()
英文:

If you have session management, you can do something like:

session.query(
    empmasterModel.Employee_Number.label("Employee_Number"), 
    empmasterModel.Employee_Name.label("Employee_Name")
).all()

If you use Flask with no session management, you can get the session from the SQLAlchemy instance.

db = SQLAlchemy() # Usually in you app.py

And then:

db.session.query(
    empmasterModel.Employee_Number.label("Employee_Number"), 
    empmasterModel.Employee_Name.label("Employee_Name")
).all()

huangapple
  • 本文由 发表于 2023年1月6日 14:00:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定