Skip to main content

SQLAlchemy specific things for django-rest-framework

Project description

Django REST Witchcraft

Build Status

SQLAlchemy specific things for django-rest-framework

Installation

pip install django-rest-witchcraft

Example

First up, lets define some simple models:

engine = create_engine('sqlite:///:memory:', echo=True)
session = scoped_session(sessionmaker(bind=engine))

Base = declarative_base()
Base.query = session.query_property()

Base.metadata.create_all(engine)

class Group(Base):
    __tablename__ = 'groups'

    id = Column(Integer(), primary_key=True)
    name = Column(String())


class User(Base):
    __tablename__ = 'users'

    id = Column(Integer(), primary_key=True)
    name = Column(String())
    fullname = Column(String())
    password = Column(String())

    _group_id = Column('group_id', Integer(), ForeignKey('groups.id'))
    group = relationship(Group, backref='users')


class Address(Base):
    __tablename__ = 'addresses'

    id = Column(Integer(), primary_key=True)
    email_addresss = Column(String(), nullable=False)

    _user_id = Column(Integer(), ForeignKey('users.id'))
    user = relationship(User, backref='addresses')

Nothing fancy here, we have a User class that can belong to a Group instance and has many Address instances

Second, lets define a serializer for User with all the fields:

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        session = session
        fields = '__all__'

This will create the following serializer for us:

>>> serializer = UserSerializer()

>>> serializer
UserSerializer():
    id = IntegerField(allow_null=False, help_text=None, label='Id', required=True)
    name = CharField(allow_null=True, help_text=None, label='Name', max_length=None, required=False)
    fullname = CharField(allow_null=True, help_text=None, label='Fullname', max_length=None, required=False)
    password = CharField(allow_null=True, help_text=None, label='Password', max_length=None, required=False)
    group = GroupSerializer(is_nested=True, required=False):
        name = CharField(allow_null=True, help_text=None, label='Name', max_length=None, required=False)
        id = IntegerField(allow_null=False, help_text=None, label='Id', required=False)
    addresses = AddressSerializer(many=True, required=False):
        id = IntegerField(allow_null=False, help_text=None, label='Id', required=False)
        email_addresss = CharField(allow_null=False, help_text=None, label='Email_addresss', max_length=None, required=True)
    url = UriField(read_only=True)

This serializer can handle nested create, update or partial update operations.

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

django-rest-witchcraft-0.0.3.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_rest_witchcraft-0.0.3-py2.py3-none-any.whl (14.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-rest-witchcraft-0.0.3.tar.gz.

File metadata

File hashes

Hashes for django-rest-witchcraft-0.0.3.tar.gz
Algorithm Hash digest
SHA256 67cb1540a843444d60dc090f57ad705a4435f9ee7a2dd1d82416fb131f99f747
MD5 9a9186c8a27a4e361c193c85dde85b0d
BLAKE2b-256 1053853791bea029bb7ab82563dd2ab48be8b9c20a8761622f8ceda2ad0d412f

See more details on using hashes here.

File details

Details for the file django_rest_witchcraft-0.0.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_rest_witchcraft-0.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f4a52963d6b786ca0c7b2b28655a3b168780d58f2435002c25b9acf4d4839161
MD5 79702307ae4bf9a23549849200a1d1e7
BLAKE2b-256 f705a9e946529ed1630205c0441a42100fccf35cd9d94ae37ace3a014ff65c2d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page