Shortcuts

This module contains helper functions for expressing things that would otherwise be somewhat verbose or cumbersome using peewee’s APIs. There are also helpers for serializing models to dictionaries and vice-versa.

model_to_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) to a dictionary.

Examples:

  1. >>> user = User.create(username='charlie')
  2. >>> model_to_dict(user)
  3. {'id': 1, 'username': 'charlie'}
  4. >>> model_to_dict(user, backrefs=True)
  5. {'id': 1, 'tweets': [], 'username': 'charlie'}
  6. >>> t1 = Tweet.create(user=user, message='tweet-1')
  7. >>> t2 = Tweet.create(user=user, message='tweet-2')
  8. >>> model_to_dict(user, backrefs=True)
  9. {
  10. 'id': 1,
  11. 'tweets': [
  12. {'id': 1, 'message': 'tweet-1'},
  13. {'id': 2, 'message': 'tweet-2'},
  14. ],
  15. 'username': 'charlie'
  16. }
  17. >>> model_to_dict(t1)
  18. {
  19. 'id': 1,
  20. 'message': 'tweet-1',
  21. 'user': {
  22. 'id': 1,
  23. 'username': 'charlie'
  24. }
  25. }
  26. >>> model_to_dict(t2, recurse=False)
  27. {'id': 1, 'message': 'tweet-2', 'user': 1}

dict_to_model(model_class, data[, ignore_unknown=False])

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 related instances where appropriate.

Examples:

  1. >>> user_data = {'id': 1, 'username': 'charlie'}
  2. >>> user = dict_to_model(User, user_data)
  3. >>> user
  4. <__main__.User at 0x7fea8fa4d490>
  5. >>> user.username
  6. 'charlie'
  7. >>> note_data = {'id': 2, 'text': 'note text', 'user': user_data}
  8. >>> note = dict_to_model(Note, note_data)
  9. >>> note.text
  10. 'note text'
  11. >>> note.user.username
  12. 'charlie'
  13. >>> user_with_notes = {
  14. ... 'id': 1,
  15. ... 'username': 'charlie',
  16. ... 'notes': [{'id': 1, 'text': 'note-1'}, {'id': 2, 'text': 'note-2'}]}
  17. >>> user = dict_to_model(User, user_with_notes)
  18. >>> user.notes[0].text
  19. 'note-1'
  20. >>> user.notes[0].user.username
  21. 'charlie'

update_model_from_dict(instance, data[, ignore_unknown=False])

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.