-
Notifications
You must be signed in to change notification settings - Fork 428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed signals can not connect to OneToOneField (#572) #573
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b99abe3
Fixed signals can not connect to OneToOneField (#572)
954-Ivory 127fe2e
Remove unused import.
954-Ivory 82d3979
Changed `PeriodicTasks.pre_save` and `PeriodicTasks.pre_delete` in `P…
954-Ivory bc790e6
Update django_celery_beat/models.py
auvipy df24abd
Append the unit-test
954-Ivory 8dd73d4
Correction of notes
954-Ivory 95763dd
Append test-case for: O2O-rel instance be deleted
954-Ivory 709c5c3
Add the docs
954-Ivory a3c8aa2
Format code according to `pycodestyle`
954-Ivory File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
def signals_connect(): | ||
from django.db.models import signals, OneToOneRel | ||
from .models import ClockedSchedule, PeriodicTask, PeriodicTasks, IntervalSchedule, CrontabSchedule, SolarSchedule | ||
|
||
def o2o_discover(): | ||
""" | ||
Description: Discover the `OneToOneField`, and connect their signals to `PeriodicTasks.changed`. | ||
|
||
Issues: Signals can not connect to OneToOneField. | ||
https://github.com/celery/django-celery-beat/issues/572 | ||
|
||
Note: The inheritance relationship introduces links between the child model and each of its | ||
parents (via an automatically-created OneToOneField). | ||
https://docs.djangoproject.com/en/stable/topics/db/models/#multi-table-inheritance | ||
""" | ||
related_objects = PeriodicTask._meta.related_objects | ||
for obj in related_objects: | ||
if isinstance(obj, OneToOneRel): | ||
sender_class = obj.related_model | ||
signals.pre_delete.connect(PeriodicTasks.changed, sender=sender_class) | ||
signals.pre_save.connect(PeriodicTasks.changed, sender=sender_class) | ||
|
||
o2o_discover() | ||
signals.pre_delete.connect(PeriodicTasks.changed, sender=PeriodicTask) | ||
signals.pre_save.connect(PeriodicTasks.changed, sender=PeriodicTask) | ||
signals.pre_delete.connect( | ||
PeriodicTasks.update_changed, sender=IntervalSchedule) | ||
signals.post_save.connect( | ||
PeriodicTasks.update_changed, sender=IntervalSchedule) | ||
signals.post_delete.connect( | ||
PeriodicTasks.update_changed, sender=CrontabSchedule) | ||
signals.post_save.connect( | ||
PeriodicTasks.update_changed, sender=CrontabSchedule) | ||
signals.post_delete.connect( | ||
PeriodicTasks.update_changed, sender=SolarSchedule) | ||
signals.post_save.connect( | ||
PeriodicTasks.update_changed, sender=SolarSchedule) | ||
signals.post_delete.connect( | ||
PeriodicTasks.update_changed, sender=ClockedSchedule) | ||
signals.post_save.connect( | ||
PeriodicTasks.update_changed, sender=ClockedSchedule) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OneToOneRel is a private API as far as I know, can you provide a testt case of examples of what you were actually trying to acheive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test case:
Even without not overwrite
save()
, thepre_save.sender
still beMyPeriodicTask.__class__
.Because in
MyPeriodicTask
, theself
isMyPeriodicTask
.And Django use self.class to set
sender
.delete()
is the same, look this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have two way to fix it.
PeriodicTasks.changed()
inPeriodicTask.save()
andPeriodicTask.delete()
.OneToOneField
, then get thier__class__
, connect to the signal.Because
OneToOneRel
is a definite type for judge reverse related which isOnetoOneField
, so I use it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would prefer first approach
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you also add the tests as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, but I got the problem like this:
https://stackoverflow.com/questions/502916/django-how-to-create-a-model-dynamically-just-for-testing
I'm trying to solve it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is that related to this patch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I solved it.
Because I need to create the model in testing.