Skip to content

Commit

Permalink
Fix version generation
Browse files Browse the repository at this point in the history
  • Loading branch information
lvoliveira committed Nov 10, 2023
1 parent b3a58dc commit ce4c125
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
=======
History
=======
#######

0.4.0 (2013-12-11)
------------------
Expand Down
66 changes: 35 additions & 31 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Serialchemy


SQLAlchemy model serialization.
===============
===============================

Motivation
----------
Expand Down Expand Up @@ -52,12 +52,12 @@ Suppose we have an `Employee` SQLAlchemy_ model declared:
.. code-block:: python
class Employee(Base):
__tablename__ = 'Employee'
__tablename__ = "Employee"
id = Column(Integer, primary_key=True)
fullname = Column(String)
admission = Column(DateTime, default=datetime(2000, 1, 1))
company_id = Column(ForeignKey('Company.id'))
company_id = Column(ForeignKey("Company.id"))
company = relationship(Company)
company_name = column_property(
select([Company.name]).where(Company.id == company_id)
Expand All @@ -70,26 +70,29 @@ Suppose we have an `Employee` SQLAlchemy_ model declared:
from serialchemy import ModelSerializer
emp = Employee(fullname='Roberto Silva', admission=datetime(2019, 4, 2))
emp = Employee(fullname="Roberto Silva", admission=datetime(2019, 4, 2))
serializer = ModelSerializer(Employee)
serializer.dump(emp)
>> {'id': None,
'fullname': 'Roberto Silva',
'admission': '2019-04-02T00:00:00',
'company_id': None,
'company_name': None,
'password': None
}
# >>
{
"id": None,
"fullname": "Roberto Silva",
"admission": "2019-04-02T00:00:00",
"company_id": None,
"company_name": None,
"password": None,
}
New items can be deserialized by the same serializer:

.. code-block:: python
new_employee = {'fullname': 'Jobson Gomes', 'admission': '2018-02-03'}
new_employee = {"fullname": "Jobson Gomes", "admission": "2018-02-03"}
serializer.load(new_employee)
>> <Employee object at 0x000001C119DE3940>
# >> <Employee object at 0x000001C119DE3940>
Serializers do not commit into the database. You must do this by yourself:

Expand All @@ -110,19 +113,19 @@ For anything beyond `Generic Types`_ we must extend the `ModelSerializer` class:
class EmployeeSerializer(ModelSerializer):
password = Field(load_only=True) # passwords should be only deserialized
password = Field(load_only=True) # passwords should be only deserialized
company = NestedModelField(Company) # dump company as nested object
serializer = EmployeeSerializer(Employee)
serializer.dump(emp)
>> {'id': 1,
'fullname': 'Roberto Silva',
'admission': '2019-04-02T00:00:00',
'company': {'id': 3,
'name': 'Acme Co'
}
}
# >>
{
"id": 1,
"fullname": "Roberto Silva",
"admission": "2019-04-02T00:00:00",
"company": {"id": 3, "name": "Acme Co"},
}
Extend Polymorphic Serializer
Expand All @@ -138,32 +141,33 @@ assume it have a joined table inheritance:
...
type = Column(String(50))
__mapper_args__ = {
'polymorphic_identity':'employee',
'polymorphic_on':type
}
__mapper_args__ = {"polymorphic_identity": "employee", "polymorphic_on": type}
class Engineer(Employee):
__tablename__ = 'Engineer'
id = Column(Integer, ForeignKey('employee.id'), primary_key=True)
__tablename__ = "Engineer"
id = Column(Integer, ForeignKey("employee.id"), primary_key=True)
association = relationship(Association)
__mapper_args__ = {
'polymorphic_identity':'engineer',
"polymorphic_identity": "engineer",
}
To use a extended `ModelSerializer` class on the `Engineer` class, you should create
the serializer as it follows:

.. code-block:: python
class EmployeeSerializer(PolymorphicModelSerializer): # Since this class will be polymorphic
class EmployeeSerializer(
PolymorphicModelSerializer
): # Since this class will be polymorphic
password = Field(load_only=True)
company = NestedModelField(Company)
class EngineerSerializer(EmployeeSerializer):
__model_class__ = Engineer # This is the table Serialchemy will refer to
__model_class__ = Engineer # This is the table Serialchemy will refer to
association = NestedModelField(Association)
Contributing
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
setup(
author="ESSS",
author_email="[email protected]",
use_scm_version={"git_describe_command": "git describe --dirty --tags --long --match v*"},
setup_requires=["setuptools_scm"],
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
Expand All @@ -44,6 +46,7 @@
include_package_data=True,
python_requires=">=3.6",
keywords="serialchemy",
data_files=[("", ["LICENSE"])],
name="serialchemy",
packages=find_packages(where="src"),
package_dir={"": "src"},
Expand Down

0 comments on commit ce4c125

Please sign in to comment.