本文整理匯總了Python中django.db.models.signals.post_init方法的典型用法代碼示例。如果您正苦於以下問題:Python signals.post_init方法的具體用法?Python signals.post_init怎麽用?Python signals.post_init使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.db.models.signals
的用法示例。
在下文中一共展示了signals.post_init方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_model_pre_init_and_post_init
# 需要導入模塊: from django.db.models import signals [as 別名]
# 或者: from django.db.models.signals import post_init [as 別名]
def test_model_pre_init_and_post_init(self):
data = []
def pre_init_callback(sender, args, **kwargs):
data.append(kwargs['kwargs'])
signals.pre_init.connect(pre_init_callback)
def post_init_callback(sender, instance, **kwargs):
data.append(instance)
signals.post_init.connect(post_init_callback)
p1 = Person(first_name="John", last_name="Doe")
self.assertEqual(data, [{}, p1])
示例2: test_invalid_sender_model_name
# 需要導入模塊: from django.db.models import signals [as 別名]
# 或者: from django.db.models.signals import post_init [as 別名]
def test_invalid_sender_model_name(self):
msg = "Invalid model reference 'invalid'. String model references must be of the form 'app_label.ModelName'."
with self.assertRaisesMessage(ValueError, msg):
signals.post_init.connect(self.receiver, sender='invalid')
示例3: test_already_loaded_model
# 需要導入模塊: from django.db.models import signals [as 別名]
# 或者: from django.db.models.signals import post_init [as 別名]
def test_already_loaded_model(self):
signals.post_init.connect(
self.receiver, sender='signals.Book', weak=False
)
try:
instance = Book()
self.assertEqual(self.received, [{
'signal': signals.post_init,
'sender': Book,
'instance': instance
}])
finally:
signals.post_init.disconnect(self.receiver, sender=Book)
示例4: test_not_loaded_model
# 需要導入模塊: from django.db.models import signals [as 別名]
# 或者: from django.db.models.signals import post_init [as 別名]
def test_not_loaded_model(self, apps):
signals.post_init.connect(
self.receiver, sender='signals.Created', weak=False, apps=apps
)
try:
class Created(models.Model):
pass
instance = Created()
self.assertEqual(self.received, [{
'signal': signals.post_init, 'sender': Created, 'instance': instance
}])
finally:
signals.post_init.disconnect(self.receiver, sender=Created)
示例5: test_disconnect
# 需要導入模塊: from django.db.models import signals [as 別名]
# 或者: from django.db.models.signals import post_init [as 別名]
def test_disconnect(self, apps):
received = []
def receiver(**kwargs):
received.append(kwargs)
signals.post_init.connect(receiver, sender='signals.Created', apps=apps)
signals.post_init.disconnect(receiver, sender='signals.Created', apps=apps)
class Created(models.Model):
pass
Created()
self.assertEqual(received, [])