使用 SQLAlchemy 保存到 mysql 后,每个单词的首字母都会大写

First letter of every word gets capitalized after saving to mysql using SQLAlchemy

我正在尝试使用 Flask+SQLAlchemy 构建休息 api。我是新来的。我刚遇到有线问题。每个单词的第一个字母在单词保存到 mysql 之后得到 capitalized。代码如下:

@app.route('/gifs')
def crawlHome():
    url = "http://domain.com.cn"
    newgif = Gif(url)
    db.session.add(newgif)
    db.session.commit() 



class Gif(db.Model):
    __tablename__ = "gifs"
    uid = db.Column(db.Integer, primary_key = True)
    url = db.Column(db.String(200))
    def __init__(self, url):
        self.url = url.title()
    @property
    def serialize(self):
        return {
            'uid' : self.uid,
            'url' : self.url
        }

而保存的url是这样的:"Http://Domain.Com.Cn"。为什么?我对 python 很陌生。所以,请原谅我的无知。

您的 url 是大写的,因为您使用的是字符串内置类型的 title 函数。

>>> "http://domain.com.cn".title()
'Http://Domain.Com.Cn'

您需要在 Gif 模型 __init__ 函数中将 self.url = url.title() 替换为 self.url = url