Django migrations and deconstructible string

杀马特。学长 韩版系。学妹 提交于 2019-12-08 16:38:32

Unfortunately it looks like object that have a deconstruct() method don't have priority over str subclasses.

What you could could here is use the django.db.migrations.writer.SettingsReference class which looks like it has a priority over str.

What I suggest you do instead is create a custom field subclass that will default to your conf value. For example, your Campaign.prefix_subject could be an instance of this class:

class PrefixSubject(models.BooleanField):
    default_help_text = (
        'Wheter to prefix the subject with "{}" or not.' % conf.SUBJECT_PREFIX
    )

    def __init__(self, *args, **kwargs):
        kwargs.setdefault('help_text', default_help_text)
        super().__init__(*args, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        if kwargs['help_text'] == self.default_help_text:
            kwargs.pop('help_text')
        return name, path, args, kwargs
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!