Django - Signals multiple values for keyword argument 'sender'
def pre_save(self, model_instance, add):
value = super(MediaUploadField, self).pre_save(mo开发者_高级运维del_instance, add)
if value and add:
post_save.connect(self.someCallback, sender=model_instance.__class__, dispatch_uid='media_attachment_signal')
return value
def someCallback(sender, **kwargs):
print "callback"
print sender
return
Is throwing the following error:
someCallback() got multiple values for keyword argument 'sender'
I honestly can't work out what I'm doing wrong, I followed the documentation precisely. I tried replacing model_instance.class with the actual class import but it throws the same error.
Does any have any idea whats wrong with my code?
It seems that someCallback
is a model method. The first argument to model methods is always the instance itself - which is usually referenced as self
. But you've called the first argument sender
- so Python is trying to receive sender
both as the first positional argument, and as one of the keyword arguments.
The best way to solve this is to define someCallback
as a staticmethod, as these don't take the instance or class:
@staticmethod
def someCallback(sender, **kwargs):
Also note that connecting your post_save handler in a pre_save method is a very strange thing to do. Don't forget that connecting a signal is a permanent thing - it's not something that's done on a per-call basis.
精彩评论