from app.models import Setting


class AppSettings(dict):
    def __init__(self):
        super().__init__()
        self.update({setting.name: setting.value for setting in Setting.query.all()})
        self.__setitem__('posts_per_page', int(self.__getitem__('posts_per_page')))

    def __setitem__(self, key, value):
        super().__setitem__(key, value)

        setting = Setting.query.filter_by(name=key).first()
        if setting is not None:
            setting.value = value
        else:
            setting = Setting(name=key, value=value)
        setting.save()

    def __setattr__(self, key, value):
        self.__setitem__(key, value)

from app.models import Setting


class AppSettings(dict):
    def __init__(self):
        super().__init__()
        self.update({setting.name: setting.value for setting in Setting.query.all()})

        try:
            self.__setitem__('posts_per_page', int(self.__getitem__('posts_per_page')))
        except KeyError:
            pass

    def __setitem__(self, key, value):
        super().__setitem__(key, value)

        setting = Setting.query.filter_by(name=key).first()
        if setting is not None:
            setting.value = value
        else:
            setting = Setting(name=key, value=value)
        setting.save()

    def __setattr__(self, key, value):
        self.__setitem__(key, value)

Add error handling for posts_per_page type conversion