Django Object Update
dj-obj-update is a module that helps you do two things while updating
an object:
- Only do a save if something changed
- Log what changed (the logger name is
obj_updateand only outputslogging.DEBUG)
Installation
pip install dj-obj-update
Usage
Updating an object
from obj_update import obj_update
new_data = {
'flavor': 'chocolate',
}
for obj in queryset:
obj_update(obj, new_data)
Dry run updating an object
from obj_update import obj_update
logger.setLevel(logging.DEBUG) # see "Logging changes" below
new_data = {
'flavor': 'chocolate',
}
for obj in queryset:
obj_update(obj, new_data, update_fields=[])
Replacement for update_or_create
from obj_update import obj_update_or_create
choice, created = obj_update_or_create(
Choice,
question=question,
defaults={'choice_text': 'Flour or corn?'},
)
Dealing with auto_now fields
By default, dj-obj-update constructs an update_fields when it saves.
This means fields like the primary key, auto_now, and auto_now_add
might not get saved. If you need these, you should set
update_fields=None. Usage is the same as Django's update_fields
Logging changes
Using python-json-logger:
import logging
from pythonjsonlogger.jsonlogger import JsonFormatter
logger = logging.getLogger('obj_update')
handler = logging.FileHandler('log/my_obj_changes.log')
handler.setFormatter(JsonFormatter())
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
With JSON logging, you'll get messages like:
{"message": "[text hello->hello2]", "model": "FooModel", "pk": 1, "changes": {"text": {"old": "hello", "new": "hello2"}}}
With a normal logger, you'll still get output, but it won't have as much information:
[text hello->hello2]
You can selectively log object creation or updates. For example, if you don't
expect objects to be created, you can only enable the obj_update.create
logger. Likewise, you can only enable the obj_update.update logger to ignore
creation. See the logging.config example at the top of
test_obj_update for an example.
Metadata
Release files for dj-obj-update 0.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| dj-obj-update-0.6.0.tar.gz | 10.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dj_obj_update-0.6.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.4 kB
Release files / dj-obj-update-0.6.0.tar.gz
| Download URL | dj-obj-update-0.6.0.tar.gz |
|---|---|
| Size | 10.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
253c7f104006f99d72b5fc2601c54e1791eefb7d1cfbeaee89ebf194df31ff68
|
|
BLAKE2b-256 checksum How to use checksums |
5a42bbd4add8d1c4091b3fbc3c3af25f4a7577f135d9d48df013ac3e28d40f86
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.24.0
|
Release files / dj_obj_update-0.6.0-py3-none-any.whl
| Download URL | dj_obj_update-0.6.0-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c9a3cbc3b4bab96b353fd807c20741d2a1e80c97704bcb14b2c6b3f6e87d1081
|
|
BLAKE2b-256 checksum How to use checksums |
b585af2a09e5f368b8f841aa2f414ed8c3ff26bc3775b5d26289b7d17bad05cf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.24.0
|
Security for dj-obj-update 0.6.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.