I have a model of article which will have slug based on it's title, the model is like this:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Text
Base = declarative_base()
class Article(Base):
__tablename__ = 'article'
id = Column(Integer, primary_key=True)
title = Column(String(100), nullable=False)
content = Column(Text)
slug = Column(String(100), nullable=False,
default=lambda c: c.current_params['title'],
onupdate=lambda c: c.current_params['title'])
slug
is taking title's value. So, everytime article slug
will match it's title. But, when I edit the content without changing it's title, this
exception is raised
(builtins.KeyError) 'title' [SQL: 'UPDATE article SET content=?, slug=?,
updated_at=? WHERE article = ?'] [parameters: [{'article_id': 1,
'content': 'blah blah blah'}]]
I guess that because current_params
doesn't contain title
. If, I change
the lambda
there and using if
, slug will be None
. So how can I handle
this and keep the slug value match it's title?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…