-
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
Conversation
This pull request introduces 1 alert when merging b99abe3 into d4b97af - view on LGTM.com new alerts:
|
django_celery_beat/signals.py
Outdated
@@ -0,0 +1,41 @@ | |||
def signals_connect(): | |||
from django.db.models import signals, OneToOneRel |
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:
# admin.py
class MyPeriodicTaskInline(admin.StackedInline):
model = MyPeriodicTask
# ...
# I want to render a `StackedInline` of `MyPeriodicTask` in `OtherModelAdmin` with my scene..
# But it's not important.
class OtherModelAdmin(admin.ModelAdmin):
model = OtherModel
inlines = (
MyPeriodicTaskInline,
)
# models.py
class MyPeriodicTask(PeriodicTask):
other_model = models.ForeignKey(
'OtherModel',
null=True,
on_delete=models.CASCADE
)
def save(self, *args, **kwargs):
self.kwargs = json.dumps('a_field_key',self.other_model.a_field)
super().save(*args, **kwargs)
# The bug is in it:
# This `save()` will call the `save_base()`.
# But in `save_base()`, it will set `pre_save.sender = MyPeriodicTask`, not `PeriodicTask`.
# So, the `PeriodicTask` can not receive signal with
# [signals.pre_delete.connect(PeriodicTasks.changed, sender=PeriodicTask)](https://github.com/celery/django-celery-beat/blob/d4b97af62889527582194f3932e013ad972a88b3/django_celery_beat/models.py#L625)
# [signals.pre_save.connect(PeriodicTasks.changed, sender=PeriodicTask)](https://github.com/celery/django-celery-beat/blob/d4b97af62889527582194f3932e013ad972a88b3/django_celery_beat/models.py#L625)
# query
my_periodic_task = MyPeriodicTask.first()
my_periodic_task.enabled = not my_periodic_task.enabled
my_periodic_task.save()
# the `PeriodicTasks.changed` will not be call.
Even without not overwrite save()
, the pre_save.sender
still be MyPeriodicTask.__class__
.
Because in MyPeriodicTask
, the self
is MyPeriodicTask
.
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.
- Don't use signal, call
PeriodicTasks.changed()
inPeriodicTask.save()
andPeriodicTask.delete()
. - Like this PR, use reverse related to get all
OneToOneField
, then get thier__class__
, connect to the signal.
Because OneToOneRel
is a definite type for judge reverse related which is OnetoOneField
, 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.
can you also add the tests as well
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.
is that related to this patch?
Yep, I solved it.
Because I need to create the model in testing.
…eriodicTasks.save` and `PeriodicTasks.delete`
looking for now-outdated files... none found ==============================
|
I consolidated some files about |
the doc gen expects some documentation about the newly created module |
Right, I will add the docs. |
some minor Error: ERROR: flake8: commands failed |
It seems like some code specification problem. |
lets try this and report if anything goes wrong |
can you help reviewing #563? |
Incidentally, the file storage of the signal has been changed.