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:

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

  • dictto_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 relatedinstances 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.  
  6. >>> user.username
  7. 'charlie'
  8.  
  9. >>> note_data = {'id': 2, 'text': 'note text', 'user': user_data}
  10. >>> note = dict_to_model(Note, note_data)
  11. >>> note.text
  12. 'note text'
  13. >>> note.user.username
  14. 'charlie'
  15.  
  16. >>> user_with_notes = {
  17. ... 'id': 1,
  18. ... 'username': 'charlie',
  19. ... 'notes': [{'id': 1, 'text': 'note-1'}, {'id': 2, 'text': 'note-2'}]}
  20. >>> user = dict_to_model(User, user_with_notes)
  21. >>> user.notes[0].text
  22. 'note-1'
  23. >>> user.notes[0].user.username
  24. 'charlie'
  • updatemodel_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.