2012-03-09 81 views
0

我想写一个视图/ SQLAlchemy的查询,让我来显示数据,多数民众赞成链接为一个ForeignKey和也是我有一个多对多的关系SQLAlchemy的查询外键字段(S)

有机型..

#Samples 
class Sample(Base): 
    __tablename__ = 'samples' 
    id = Column(Integer, primary_key=True) 
    name = Column(Unicode) 

    def __unicode__(self): 
     return self.name 

samples_to_customer = Table('samples_to_customer', Base.metadata, 
    Column('customer_id', Integer, ForeignKey('customer.id')), 
    Column('sample_id', Integer, ForeignKey('samples.id')) 
) 

#Create a cusotmer model to store customer numbers 
class Customer(Base): 
    __tablename__ = 'customer' 
    id = Column(Integer, primary_key=True) 
    name = Column(Unicode, unique=True) #this will be the variable used to search the existing db 
    customer_samples = relationship('Sample', secondary='samples_to_customer', backref='samples') 
    sales_rep = Column(Integer, ForeignKey('rep.id')) 
    Rep = relationship('Rep') 

    def __unicode__(self): 
     return self.name 

# This model will have its own entry view/page a user logs on and enters notes (according to a customer 
# number) they then get stored/saved onto the Customers account 
class Note(Base): 
    __tablename__ = 'note' 
    id = Column(Integer, primary_key=True) 
    name = Column(Unicode) 
    pub_date = Column(Date) 
    customer_no = Column(Integer, ForeignKey('customer.id')) 
    customer = relationship('Customer') 

    def __unicode__(self): 
     return self.name 

而且观点..

@view_config(route_name='view_customer', renderer='templates/view_customer.jinja2') 
def view_page(request): 
    customer_no = request.matchdict['customer'] 
    cust_slcustm = DBSessionRO.query(Slcustm).filter(Slcustm.customer == customer_no).first() 
    cust_customer = DBSessionRO.query(Custom).filter(Custom.cu_custref== customer_no).first() 

    # Return a 404 if a result isn't found, slcustm and customer share one2one relationship on customer_no 
    if cust_slcustm is None: 
     return HTTPNotFound('No such customer') 

    return dict(cust_slcustm=cust_slcustm, cust_customer=cust_customer) 

但我似乎无法编写一个查询,将显示每一个与客户相关的数字样本,并注意?如果有人可以帮助我将非常感激:)

+1

这是更好如果你可以把你的代码放在这里;) – sacabuche 2012-03-09 16:37:20

+2

你试过了什么?什么工作,什么没有?你有错误吗?你收到错误的数据了吗? – katrielalex 2012-03-09 16:58:19

回答

0

什么结束了工作是:

#views.py 
@view_config(route_name='view_customer', renderer='templates/view_customer.jinja2') 
def view_page(request): 
    customer_no = request.matchdict['customer'] 
    cust_slcustm = DBSessionRO.query(Slcustm).filter(Slcustm.customer == customer_no).first() 
    cust_customer = DBSessionRO.query(Custom).filter(Custom.cu_custref== customer_no).first() 
    cust_books = DBSessionRW.query(Customer).filter(Customer.name == customer_no).first() 

    # Return a 404 if a result isn't found, slcustm and customer share one2one relationship on customer_no 
    if cust_slcustm is None: 
     return HTTPNotFound('No such customer') 

    return dict(cust_slcustm=cust_slcustm, cust_customer=cust_customer, cust_books=cust_books) 

然后创建模板:

{% for sample in cust_books.customer_samples %} 
    {{ sample.name }} 
{% endfor %} 
1

试试这个

class Note(Base): 
    __tablename__ = 'note' 
    id = Column(Integer, primary_key=True) 
    name = Column(Unicode) 
    pub_date = Column(Date) 
    customer_no = Column(Integer, ForeignKey('customer.id')) 
    customer = relationship('Customer', backref='notes') 

@view_config(route_name='view_customer', renderer='templates/view_customer.jinja2') 
def view_page(request): 
    customer_no = request.matchdict['customer'] 
    cust = DBSessionRO.query(Customer).filter(Customer.id == customer_no).first() 

    print "Sample :", cust.customer_sample 
    print "Notes :", cust.notes 
+0

backref帮助我进行了另一次斗争,谢谢! :) – Crooksey 2012-03-12 15:05:21