Fork me on GitHub Fork me on Gitee

SQLAlchemy连接数据库并在django admin显示

SQLAlchemy 0.7     postgersql  9.0

SQLAlchemy连接数据库有两种方法,一种是classic,一种是modern

1,modern方法

 1 from sqlalchemy import create_engine
 2 from sqlalchemy.ext.declarative import declarative_base
 3 from sqlalchemy import Column, Integer, String
 4 from sqlalchemy.orm import sessionmaker
 5 from sqlalchemy import Sequence
 6 
 7 #绑定数据库
 8 engine = create_engine('postgres://postgres:1@localhost/fy', echo=False)
 9 Base = declarative_base()
10 
11 class User(Base):
12     __tablename__ = 'users'
13     id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
14     name = Column(String)
15     fullname = Column(String)
16     password = Column(String)
17     
18     def __init__(self, name, fullname, password):
19         self.name = name
20         self.fullname = fullname
21         self.password = password
22         
23     def __repr__(self):
24         return "<User('%s','%s', '%s')>" % (self.name, self.fullname, self.password)
25     
26     
27 Base.metadata.create_all(engine)
28 
29 #session会话
30 Session = sessionmaker(bind=engine)
31 Session = sessionmaker()
32 Session.configure(bind=engine)
33 session = Session()
34 
35 user_name = raw_input('name >')
36 user_fullname = raw_input('fullname >')
37 user_password = raw_input('password >')
38 
39 ed_user = User(user_name, user_fullname, user_password)
40 session.add(ed_user)
41 
42 #提交
43 session.commit() 
44 session.close()

 

2. classic方法 也可以用session方法增加数据

 1 from sqlalchemy import Table, MetaData, Column, Integer, String
 2 from sqlalchemy.orm import mapper
 3 from sqlalchemy import create_engine
 4 from sqlalchemy.ext.declarative import declarative_base
 5 from sqlalchemy import Column, Integer, String
 6 from sqlalchemy.orm import sessionmaker
 7 from sqlalchemy import Sequence
 8 
 9 engine = create_engine('postgres://postgres:1@localhost/fy', echo=False)
10 metadata = MetaData()
11  
12 user = Table('users', metadata,
13             Column('id', Integer, Sequence('user_id_seq'), primary_key=True),
14             Column('name', String(50)),
15             Column('fullname', String(50)),
16             Column('password', String(12))
17         ) 
18 
19 metadata.create_all(engine, checkfirst=True)
20 conn = engine.connect()  
21 
22 class User(object):
23     def __init__(self, name, fullname, password):
24         self.name = name
25         self.fullname = fullname
26         self.password = password 
27      
28 mapper(User, user) 
29 
30 user_name = raw_input('name >')
31 user_fullname = raw_input('fullname >')
32 user_password = raw_input('password >')
33 
34 ed_user = user.insert(values=dict(name=user_name,fullname=user_fullname,password=user_password))
35 conn.execute(ed_user)
36 conn.close()

 

在django admin显示:

先创建一个app,然后按照django的教程弄出django管理(表名不是这个例子的)

只要SQLAlchemy创建的表和model.py的表名一样,就可以显示出来了。

model.py

 1 from django.db import models
 2 
 3 class User(models.Model):
 4     id = models.AutoField(primary_key=True)
 5     name = models.CharField(max_length=20)
 6     fullname = models.CharField(max_length=20)
 7     password = models.CharField(max_length=20)
 8  
 9     def __unicode__(self):
10         return self.name    

 

参考资料:

http://wangye.org/blog/archives/date/2012/10

 http://www.cnblogs.com/fangyu19900812/admin/EditPosts.aspx?opt=1    (这个要慢慢找)

http://docs.sqlalchemy.org/en/rel_0_7/     官方文档

http://www.blogjava.net/sean/archive/2009/01/26/252597.html

 

 

posted @ 2013-08-27 16:22  伴途の永远  阅读(706)  评论(0编辑  收藏  举报