Skip to content

Latest commit

 

History

History
103 lines (77 loc) · 4 KB

File metadata and controls

103 lines (77 loc) · 4 KB

title: sqlalchemy.orm backref Example Code category: page slug: sqlalchemy-orm-backref-examples sortorder: 500031063 toc: False sidebartitle: sqlalchemy.orm backref meta: Python example code that shows how to use the backref callable from the sqlalchemy.orm module of the SQLAlchemy project.

backref is a callable within the sqlalchemy.orm module of the SQLAlchemy project.

ColumnProperty, CompositeProperty, Load, Mapper, Query, RelationshipProperty, Session, SynonymProperty, aliased, attributes, class_mapper, column_property, composite, interfaces, mapper, mapperlib, object_mapper, object_session, query, relationship, session, sessionmaker, and strategies are several other callables with code examples from the same sqlalchemy.orm package.

Example 1 from sqlalchemy-datatables

sqlalchemy-datatables (PyPI package information) is a helper library that makes it easier to use SQLAlchemy with the jQuery JavaScript DataTables plugin. This library is designed to be web framework agnostic and provides code examples for both Flask and Pyramid.

The project is built and maintained by Michel Nemnom (Pegase745) and is open sourced under the MIT license.

sqlalchemy-datatables / tests / models.py

# models.py
import datetime

from sqlalchemy import Column, Date, DateTime, ForeignKey, Integer, String, func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
~~from sqlalchemy.orm import backref, relationship

Base = declarative_base()


class User(Base):

    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    name = Column(String, unique=True)
    created_at = Column(DateTime, default=datetime.datetime.utcnow)
    birthday = Column(Date)
~~    address = relationship('Address', uselist=False, backref=backref('user'))

    def __unicode__(self):
        return '%s' % self.name

    def __repr__(self):
        return '<%s#%s>' % (self.__class__.__name__, self.id)

    @hybrid_property
    def dummy(self):
        return self.name[0:3]

    @dummy.expression
    def dummy(cls):
        return func.substr(cls.name, 0, 3)


class Address(Base):

    __tablename__ = 'addresses'

    id = Column(Integer, primary_key=True)
    description = Column(String, unique=True)
    user_id = Column(Integer, ForeignKey('users.id'))



## ... source file continues with no further backref examples...