Snippet List
Enables convenient adding of fields, methods and properties to Django models.
Instead of:
User.add_to_class('foo', models.CharField(...)
User.add_to_class('bar', models.IntegerField(...)
you can write:
class UserMixin(ModelMixin):
model = User
foo = models.CharField(...)
bar = models.IntegerField(...)
- mixin
- metaclass
- util
- metaprogramming
When called, this module dynamically alters the behaviour of model.save() on a list of models so that the SQL is returned and aggregated for a bulk commit later on. This is much faster than performing bulk writing operations using the standard model.save().
To use, simply save the code as django_bulk_save.py and replace this idiom:
for m in model_list:
# modify m ...
m.save() # ouch
with this one:
from django_bulk_save import DeferredBucket
deferred = DeferredBucket()
for m in model_list:
# modify m ...
deferred.append(m)
deferred.bulk_save()
Notes:
* - After performing a bulk_save(), the id's of the models do not get automatically updated, so code that depends on models having a pk (e.g. m2m assignments) will need to reload the objects via a queryset.
* - post-save signal is not sent. see above.
* - This code has not been thoroughly tested, and is not guaranteed (or recommended) for production use.
* - It may stop working in newer django versions, or whenever django's model.save() related code gets updated in the future.
- sql
- tool
- dump
- save
- db
- bulk
- util
2 snippets posted so far.