From 981b3a7ebd9c7f7deaeec97d0f72c0211a2c2b70 Mon Sep 17 00:00:00 2001 From: Konsta Vesterinen Date: Thu, 27 Mar 2014 14:16:33 +0200 Subject: [PATCH] Add docs for IPAddressType --- sqlalchemy_utils/types/country.py | 9 ++++++++- sqlalchemy_utils/types/ip_address.py | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/sqlalchemy_utils/types/country.py b/sqlalchemy_utils/types/country.py index 5032cc8..cb689f9 100644 --- a/sqlalchemy_utils/types/country.py +++ b/sqlalchemy_utils/types/country.py @@ -56,7 +56,7 @@ class CountryType(types.TypeDecorator, ScalarCoercible): user = User() - user.working_days = Country('FI') + user.country = Country('FI') session.add(user) session.commit() @@ -64,6 +64,13 @@ class CountryType(types.TypeDecorator, ScalarCoercible): user.country.name # Finland print user.country # Finland + + + CountryType is scalar coercible:: + + + user.country = 'US' + user.country # Country('US') """ impl = types.String(2) python_type = Country diff --git a/sqlalchemy_utils/types/ip_address.py b/sqlalchemy_utils/types/ip_address.py index de0db23..7696409 100644 --- a/sqlalchemy_utils/types/ip_address.py +++ b/sqlalchemy_utils/types/ip_address.py @@ -19,6 +19,32 @@ class IPAddressType(types.TypeDecorator, ScalarCoercible): """ Changes IPAddress objects to a string representation on the way in and changes them back to IPAddress objects on the way out. + + IPAddressType uses ipaddress package on Python >= 3 and ipaddr_ package on + Python 2. In order to use IPAddressType with python you need to install + ipaddr_ first. + + .. _ipaddr: https://pypi.python.org/pypi/ipaddr + + :: + + + from sqlalchemy_utils import IPAddressType + + + class User(Base): + __tablename__ = 'user' + id = sa.Column(sa.Integer, autoincrement=True) + name = sa.Column(sa.Unicode(255)) + ip_address = sa.Column(IPAddressType) + + + user = User() + user.ip_address = '123.123.123.123' + session.add(user) + session.commit() + + user.ip_address # IPAddress object """ impl = types.Unicode(50)