【问题标题】:Wagtail: Changing the default setting for "UserProfile.submitted_notifications" from "True" to "False"Wagtail:将“UserProfile.submitted_notifications”的默认设置从“True”更改为“False”
【发布时间】:2018-10-16 20:37:01
【问题描述】:

wagtail 用户实例的默认通知设置似乎是“当页面提交审核时接收通知”,这不符合我们的需求:我们不希望每个 wagtail 用户都收到通知(通过电子邮件发送)页面被提交以供审核。

此用户通知设置的默认值为“True”:

# wagtail/wagtail/users/models.py
# https://github.com/wagtail/wagtail/blob/master/wagtail/users/models.py#L25
class UserProfile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    submitted_notifications = models.BooleanField(
        verbose_name=_('submitted notifications'),
        default=True,
        help_text=_("Receive notification when a page is submitted for moderation")
)

……我想将默认设置更改为“False”(例如,用户应该选择接收电子邮件通知)。

但我不知道这项任务的“最佳实践方法”是什么。

遇到这个问题我有点惊讶,因为我认为只有“版主”组/角色的成员会收到通知。 (这似乎不是真的)。​​


我尝试/想到的:

  • 以编程方式修改此设置(有效):

    可以通过 shell 修改现有的用户实例:

    ./manage.py shell
    from django.contrib.auth import get_user_model
    User = get_user_model()
    for user in User.objects.all():
        user.wagtail_userprofile.update(submitted_notifications=False)
        user.wagtail_userprofile.save()
    
  • 在我的 CustomUser save() 方法中修改此设置(不起作用):

    # project/users/models.py (pseudocode)
    class CustomUser(AbstractUser):
        def save(self, *args, **kwargs):
            super().save(*args, **kwargs)
    
            self.wagtail_userprofile.update(submitted_notifications=False)
            self.wagtail_userprofile.save()
    

    这会引发“RelatedObjectDoesNotExist”错误。

  • 添加数据迁移以更改默认值(只是一个想法——我不知道这是否是一个选项或如何实现)。


设置:

  • django 2.0.8
  • 鹡鸰 2.2.2

【问题讨论】:

    标签: django wagtail


    【解决方案1】:

    您可以使用后保存信号

    import UserProfile from wagtail.users.models   
    
    @receiver(post_save, sender=UserProfile)
    def update_user_profile(sender, instance, created, **kwargs):
       if created:
           instance.submitted_notifications = False
           instance.save()
    

    因此,这会在创建新配置文件并将“submitted_notifications”默认设置为 False 时触发。希望对你有帮助

    【讨论】:

    • 亲爱的 Damilola,谢谢,这将是我考虑的最后一个选项,因为我从一些以前使用的信号切换到我的自定义模型 save() 方法...我更愿意更改默认设置。
    • 信号与保存方法不同。它们在不同的时刻被解雇,因此可以用于不同的目的。有人可能会说信号很神奇,你不应该使用它们。但是将您的代码从信号转移到保存或反之亦然,这不仅是一种风格问题。 post_save 信号按照所说的做。保存后。覆盖保存 - 就像你现在做的那样 - 还为时过早。
    • 谢谢@allcaps,我知道,我真的不想在我的保存方法和信号中使用这种“默认重置”。我不喜欢在每个新实例上设置这个值,因为我无法更改这个不需要的初始默认值。但是如果无法更改默认值,我会通过信号设置。
    【解决方案2】:

    在 save 方法中更新相关配置文件引发RelatedObjectDoesNotExist 的原因是因为相关对象是在保存后创建的。 db 事务需要提交才能获得主键。显然你需要一个 pk 来存储关系。

    这就是信号起作用的原因。数据库事务完成后会触发 post save 信号。我认为信号是解决您问题的有效方法。

    您也可以在保存方法中使用transaction.on_commit。 On commit 等待事务完成。

    有时您需要执行与当前数据库事务相关的操作,但前提是事务成功提交。示例可能包括 Celery 任务、电子邮件通知、缓存失效或在相关对象中设置一些默认值。这是关于该主题的文档: https://docs.djangoproject.com/en/1.11/topics/db/transactions/#performing-actions-after-commit

    在您的模型中执行以下操作:

    from django.db import transaction
    
    class Foo(...):
        ...
    
        def save(self, *args, **kwargs):
            instance = super().save(*args, **kwargs)
            if not self.pk:
                transaction.on_commit(self.update_profile)
            return instance
    
        def update_profile(self):
            self.wagtail_userprofile.update(submitted_notifications=False)
            # Note: update, no need to call save. 
            # Signals will not be fired on behalf of the UserProfile.
    

    免责声明,我没有运行/测试此代码。

    【讨论】:

      猜你喜欢
      • 2016-07-14
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 2013-12-16
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      相关资源
      最近更新 更多