https://github.com/honmaple/maple-json
sqlalchemy serializer to json
https://github.com/honmaple/maple-json
json serializer sqlalchemy
Last synced: about 1 year ago
JSON representation
sqlalchemy serializer to json
- Host: GitHub
- URL: https://github.com/honmaple/maple-json
- Owner: honmaple
- License: bsd-3-clause
- Created: 2016-12-13T14:00:51.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-08-08T02:47:50.000Z (almost 9 years ago)
- Last Synced: 2025-03-20T16:52:09.672Z (over 1 year ago)
- Topics: json, serializer, sqlalchemy
- Language: Python
- Size: 11.7 KB
- Stars: 9
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.org
- License: LICENSE
Awesome Lists containing this project
README
* maple-json
[[LICENSE][https://img.shields.io/badge/license-BSD-blue.svg]]
[[https://www.python.org/download/releases/3.0/][https://img.shields.io/badge/python-3.5-green.svg]]
* Serializer sqlachemy object to json
** usage
it's simple to use(take flask-sqlalchemy as an example)
** multi instances
#+BEGIN_SRC python
posts = Post.query.all()
serializer = Serializer(posts)
data = serializer.data
#+END_SRC
** single instance
#+BEGIN_SRC python
post = Post.query.first()
serializer = Serializer(post)
data = serializer.data
#+END_SRC
** exclude some columns
#+BEGIN_SRC python
serializer = Serializer(post,exclude=['title'])
#+END_SRC
** only include some column
#+BEGIN_SRC python
serializer = Serializer(post,include=['title'])
#+END_SRC
** relation depth
#+BEGIN_SRC python
serializer = Serializer(post,depth=3)
#+END_SRC
- depth
default:2
** add some custom function
#+BEGIN_SRC python
serializer = Serializer(post,extra=['get_post_count'])
#+END_SRC
Post
#+BEGIN_SRC python
class Post(Model):
......
def get_post_count(self):
return 11
#+END_SRC
** Other
#+BEGIN_SRC python
class PostSerializer(Serializer):
count = Field(source = 'get_post_count',args={'name':'hello'},default=20)
class Meta:
include = []
depth = 2
include = []
exclude = []
extra = ['count']
#+END_SRC
* Just use sqlalchemy like django orm
** base query(have finished)
- gt
- lt
- lte
- gte
- contains
- in
- exact
- iexact
- startswith
- istartswith
- iendswith
- endswith
- isnull
- range
- year
- month
- day
Example:
#+BEGIN_SRC python
Post.query.filter_by(title__contains = 'sql').all()
Post.query.exclude_by(title__contains = 'sql').all()
#+END_SRC
** relationship query
#+BEGIN_SRC python
Post.query.filter_by(tags__name__contains = 'sql').all()
#+END_SRC
** other
#+BEGIN_SRC python
Post.query.filter_by(tags__name__contains = 'sql').or(Post.id == 1,Post.id == 2).all()
Post.query.filter_by(tags__name__contains = 'sql').and(Post.id == 1,Post.id == 2).all()
Post.query.filter_by(tags__name__contains = 'sql').exists()
Post.query.load_only('title')
#+END_SRC
* Use sqlalchemy better
take flask-sqlalchemy as an example,you can remove part of the repetitive work by use *Mixin* of 'models.py'
** ModelMixin
Increment ID -- id
#+BEGIN_SRC python
post = Post(·····)
post.save()
post.delete()
#+END_SRC
bulk operation
- bulk_insert
- bulk_update
- bulk_save
** ModelTimeMixin
- created_at
Data create time
- updated_at
Data update time
** ModelUserMixin
relate to *User*(many to one)
#+BEGIN_SRC python
class Post(ModelUserMixin, Model):
user_related_name = 'posts'
titile = ...
#+END_SRC