def add_current_site(sender, instance, **kwargs):
"""
Add the current site to a model's sites property after a save. This is
required in post save because ManyToManyField fields require an existing
key.
TODO: Don't run this on *every* post_save.
"""
if hasattr(instance, 'sites'):
if not instance.sites.all():
instance.sites.set(Site.objects.filter(id=current_site_id()))
instance.save()
评论列表
文章目录