SQLAlchemy-OrderingItem provides an OrderingItem base for children of orderinglist relationships.
Project description
SQLAlchemy-OrderingItem provides an OrderingItem
base for children of orderinglist
relationships. Children of orderinglist
relationships will exhibit more intuitive behavior when setting their parent attribute.
Installation
$ pip install sqlalchemy-orderingitem
Quickstart
from sqlalchemy_orderingitem import OrderingItem
from sqlalchemy import Column, ForeignKey, Integer, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.orderinglist import ordering_list
from sqlalchemy.orm import sessionmaker, relationship
# standard session creation
engine = create_engine('sqlite:///:memory:')
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
# declare orderinglist relationships
children = relationship(
'Child',
backref='parent',
order_by='Child.index',
collection_class=ordering_list('index')
)
# subclass OrderingItem for children of orderinglist relationships
class Child(OrderingItem, Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))
index = Column(Integer)
# create database tables and instantiate parent and child
Base.metadata.create_all(engine)
parent = Parent()
child = Child()
session.add_all([parent, child])
session.commit()
# example 1: setting a child's parent attribute to Parent object
# we expect the child's index to be 0
child.parent = parent
print(child.index)
# example 2: setting a child's parent attribute to None
# we expect the child's index to be None
parent.children = [c]
child.parent = None
print(child.index)
Out:
0
None
If Child
did not subclass OrderingItem
, we would observe the following unintuitive output:
None
0
Citation
@software{bowen2020sqlalchemy-orderingitem,
author = {Dillon Bowen},
title = {SQLAlchemy-OrderingItem},
url = {https://dsbowen.github.io/sqlalchemy-orderingitem/},
date = {2020-06-05},
}
License
Users must cite this package in any publications which use it.
It is licensed with the MIT License.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sqlalchemy-orderingitem-0.0.5.tar.gz
.
File metadata
- Download URL: sqlalchemy-orderingitem-0.0.5.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77de49484d3161fcf5905b3ad1213d4d34032def97725a3434f26776e20bfc82 |
|
MD5 | 7082527bca338c7b6ed18ced91037530 |
|
BLAKE2b-256 | 6b2a894c8a08de38a0619d5b7f95cdd81671f8824e9826927c345ce8f285539e |
File details
Details for the file sqlalchemy_orderingitem-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: sqlalchemy_orderingitem-0.0.5-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | da2f441b1179f75cc7c042c647c0f7d2527232fdfd700abb3afd1d52ac2e5fe0 |
|
MD5 | d67dd0875af498604e41001908432b00 |
|
BLAKE2b-256 | d6183bbbcdbf5fa0afb6ae304f2b784911cead05da8dc4c595ae24f2a438239e |