Shortcuts
This module contains helper functions for expressing things that wouldotherwise be somewhat verbose or cumbersome using peewee’s APIs. There are alsohelpers for serializing models to dictionaries and vice-versa.
modelto_dict
(_model[, recurse=True[, backrefs=False[, only=None[, exclude=None[, extra_attrs=None[, fields_from_query=None[, max_depth=None[, manytomany=False]]]]]]]])
Parameters:
- recurse (bool) – Whether foreign-keys should be recursed.
- backrefs (bool) – Whether lists of related objects should be recursed.
- only – A list (or set) of field instances which should be included in the result dictionary.
- exclude – A list (or set) of field instances which should be excluded from the result dictionary.
- extra_attrs – A list of attribute or method names on the instance which should be included in the dictionary.
- fields_from_query (Select) – The
SelectQuery
that created this model instance. Only the fields and values explicitly selected by the query will be serialized. - max_depth (int) – Maximum depth when recursing.
- manytomany (bool) – Process many-to-many fields.
Convert a model instance (and optionally any related instances) toa dictionary.
Examples:
- >>> user = User.create(username='charlie')
- >>> model_to_dict(user)
- {'id': 1, 'username': 'charlie'}
- >>> model_to_dict(user, backrefs=True)
- {'id': 1, 'tweets': [], 'username': 'charlie'}
- >>> t1 = Tweet.create(user=user, message='tweet-1')
- >>> t2 = Tweet.create(user=user, message='tweet-2')
- >>> model_to_dict(user, backrefs=True)
- {
- 'id': 1,
- 'tweets': [
- {'id': 1, 'message': 'tweet-1'},
- {'id': 2, 'message': 'tweet-2'},
- ],
- 'username': 'charlie'
- }
- >>> model_to_dict(t1)
- {
- 'id': 1,
- 'message': 'tweet-1',
- 'user': {
- 'id': 1,
- 'username': 'charlie'
- }
- }
- >>> model_to_dict(t2, recurse=False)
- {'id': 1, 'message': 'tweet-2', 'user': 1}
The implementation of model_to_dict
is fairly complex, owing to thevarious usages it attempts to support. If you have a special usage, Istrongly advise that you do not attempt to shoe-horn some crazycombination of parameters into this function. Just write a simple functionthat accomplishes exactly what you’re attempting to do.
Parameters:
- model_class (Model) – The model class to construct.
- data (dict) – A dictionary of data. Foreign keys can be included as nested dictionaries, and back-references as lists of dictionaries.
- ignore_unknown (bool) – Whether to allow unrecognized (non-field) attributes.
Convert a dictionary of data to a model instance, creating relatedinstances where appropriate.
Examples:
- >>> user_data = {'id': 1, 'username': 'charlie'}
- >>> user = dict_to_model(User, user_data)
- >>> user
- <__main__.User at 0x7fea8fa4d490>
- >>> user.username
- 'charlie'
- >>> note_data = {'id': 2, 'text': 'note text', 'user': user_data}
- >>> note = dict_to_model(Note, note_data)
- >>> note.text
- 'note text'
- >>> note.user.username
- 'charlie'
- >>> user_with_notes = {
- ... 'id': 1,
- ... 'username': 'charlie',
- ... 'notes': [{'id': 1, 'text': 'note-1'}, {'id': 2, 'text': 'note-2'}]}
- >>> user = dict_to_model(User, user_with_notes)
- >>> user.notes[0].text
- 'note-1'
- >>> user.notes[0].user.username
- 'charlie'
Parameters:
- instance (Model) – The model instance to update.
- data (dict) – A dictionary of data. Foreign keys can be included as nested dictionaries, and back-references as lists of dictionaries.
- ignore_unknown (bool) – Whether to allow unrecognized (non-field) attributes.
Update a model instance with the given data dictionary.