Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting the resulting dict class. #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions dictalchemy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import absolute_import, division

import copy

from sqlalchemy import inspect
from sqlalchemy.ext.associationproxy import _AssociationList

Expand Down Expand Up @@ -42,7 +43,8 @@ def arg_to_dict(arg):


def asdict(model, exclude=None, exclude_underscore=None, exclude_pk=None,
follow=None, include=None, only=None, method='asdict', **kwargs):
follow=None, include=None, only=None, method='asdict',
DictClass=dict, **kwargs):
"""Get a dict from a model

Using the `method` parameter makes it possible to have multiple methods
Expand Down Expand Up @@ -75,6 +77,7 @@ def asdict(model, exclude=None, exclude_underscore=None, exclude_pk=None,
:param method: Name of the method that is currently called. This will be \
the default method used in 'follow' unless another method is\
set.
:param DictClass: Class to be used for resulting dict.

:raises: :class:`dictalchemy.errors.MissingRelationError` \
if `follow` contains a non-existent relationship.
Expand Down Expand Up @@ -115,7 +118,7 @@ def asdict(model, exclude=None, exclude_underscore=None, exclude_pk=None,
None)) or [])
attrs = [k for k in columns + synonyms + include if k not in exclude]

data = dict([(k, getattr(model, k)) for k in attrs])
data = DictClass([(k, getattr(model, k)) for k in attrs])

for (rel_key, orig_args) in follow.iteritems():

Expand All @@ -139,7 +142,7 @@ def asdict(model, exclude=None, exclude_underscore=None, exclude_pk=None,
rel_data.append(getattr(child, method)(**args))
else:
try:
rel_data.append(dict(child))
rel_data.append(DictClass(child))
# TypeError is for non-dictable children
except TypeError:
rel_data.append(copy.copy(child))
Expand All @@ -152,7 +155,7 @@ def asdict(model, exclude=None, exclude_underscore=None, exclude_pk=None,
rel_data[child_key] = getattr(child, method)(**args)
else:
try:
rel_data[child_key] = dict(child)
rel_data[child_key] = DictClass(child)
except ValueError:
rel_data[child_key] = copy.copy(child)

Expand All @@ -163,7 +166,7 @@ def asdict(model, exclude=None, exclude_underscore=None, exclude_pk=None,
if hasattr(child, method):
rel_data.append(getattr(child, method)(**args))
else:
rel_data.append(dict(child))
rel_data.append(DictClass(child))

elif rel is None:
rel_data = None
Expand Down