#20136 closed Cleanup/optimization (fixed)
Recommend note identifying that post_save signals will be fired on loaddata
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Documentation | Version: | 1.5 |
Severity: | Normal | Keywords: | loaddata |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
On: https://docs.djangoproject.com/en/dev/ref/django-admin/#loaddata-fixture-fixture
It says the following:
- When fixture files are processed, the data is saved to the database as is. Model defined save methods and pre_save signals are not called.
However, it does not make specific mention about other signals. Recommend making the text explicit so that readers understand that other signals not mentioned here will be called (post_save et al).
Example:
Let's say you're testing on a local server and you have use post_save to send emails or other notifications. loaddata will resend all emails on load. This is behaviour that, in a test environment, one may wish to avoid.
Attachments (2)
Change History (10)
by , 12 years ago
Attachment: | 20136.diff added |
---|
comment:1 by , 12 years ago
Has patch: | set |
---|---|
Type: | Uncategorized → Cleanup/optimization |
comment:2 by , 12 years ago
Just looking at the diff, I asked a similar question to 8399 and 13299. The answer suggested creating a decorator to block the signal from loaddata. Seems like it may be a better solution than increasing cyclomatic complexity in signal functions.
from functools import wraps def disable_for_loaddata(signal_handler): """ Decorator that turns off signal handlers when loading fixture data. """ @wraps(signal_handler) def wrapper(*args, **kwargs): if kwargs['raw']: return signal_handler(*args, **kwargs) return wrapper Then: @disable_for_loaddata def your_fun(**kwargs): ## stuff that won't happen if the signal is initiated by loaddata process
comment:3 by , 12 years ago
If I am not mistaken both pre_save and post_save signals will be sent. There is a raw argument to the signal that can be used to detect when loaddata (or any other method wishing to insert the values as-is to the DB). See https://docs.djangoproject.com/en/dev/ref/signals/#django.db.models.signals.pre_save.
It seems the loaddata docs are currently plain wrong.
comment:4 by , 12 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
I wouldn't be opposed to documenting the decorator method as I've used it before as well.
comment:5 by , 12 years ago
That decorator creates some unintended consequences when used in conjunction with django-tastypie (API POST calls skip signals with that decorator). Perhaps not entirely relevant here, but "nice to know".
by , 12 years ago
Attachment: | 20136.2.diff added |
---|
comment:6 by , 12 years ago
Patch needs improvement: | unset |
---|
Anssi is correct, I've fixed that inaccuracy and added the decorator example.
comment:7 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Added a patch which also attempts to address #13299 since it's related.