'NoneType' object is not subscriptable when serializing fields that do not belong to the database is None

Author: dinghao-8Created Jun 20, 2025Updated Jan 7, 2026
Labelsquestion
python
class UserConfig(SQLModel, table=True):
    __tablename__ = "user_config"
    id: Optional[int] = Field(default=None, primary_key=True)
    user_id: Optional[int]
    _user_name: str = '' #---> not in database fields

    @property
    def user_name(self):
        return self._user_name

    @user_name.setter
    def user_name(self, user_name):
        self._user_name = user_name


class UserConfigSchema(Schema):
    user_id = fields.Int() 
    user_name = fields.Str(allow_none= True)
    id = fields.Int()

    class Meta:
        model = UserConfig
        unknown = EXCLUDE

user_config_schema.dumps(user_config) # --->issue NoneType ..... what i should to do in that time. Please confirm whether it needs to be repaired or if there are any solutions other than @post_dump

Source: marshmallow-code/marshmallow