本文介绍了在烧瓶的子弹领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个存储在数据库中的slug字段。我搜索了一下,发现,但是在将代码集成到我的应用程序中时遇到了问题。
这是我的 modele.py
:
from unicodedata import normalize
def slugfy(text,encoding = None,
allowed_chars ='abcdefghijklmnopqrstuvwxyz0123456789-'):
如果isinstance(text,str):
text = text.decode(encoding or'ascii ')
clean_text = text.strip()。replace('',' - ')。lower()
while' - 'in clean_text:
clean_text = clean_text.replace(' - ',' - ')
ascii_text = normalize('NFKD',clean_text).encode('ascii','ignore')
strict_text = map(lambda x:x if allowed_chars else (db.Model):
id = db.Column(db。整数,primary_key = True)
nom = db.Column(db.String(200),index = True,unique = True)
slug = db.Column(db.String(255))
chanteurs = db.relationship('Chanson',backref ='chanteur',lazy ='dynamic')
def __repr __(self):
return'< Chanteur%r> '%(self.nom)
$ b $ def __setattr __(self,key,value):
super(Chanteur,self).__ setattr __(key,value)
if key == 'nom':
self.slug = slugfy(self.nom)
$ b $ class Chanson(db.Model):
id = db.Column(db。整数,primary_key = True)
titre = db.Column(db.String(255))
chanteur_id = db.Column(db.Integer,db.ForeignKey('chanteur.id'))
def __repr __(self):
return'< Chanson%r>'(self.titre)
它不工作:当我添加一个新的objet(chanteur)时,slug字段是空的
解决方案
为了在数据库中保留slug,我使用下面的方法(使用非常有用的 <$ c
$ b
from slugify import slugify#among其他的东西
class Song(db.Model):
id = db.Column(db.Integer,primary_key = True)
title = db.Column(db.String( 255))
slug = db.Column(db.String(255))
def __init __(self,* args,** kwargs):
如果不是'slug' kwargs:
kwargs ['slug'] = slugify(kwargs.get('title',''))
super().__ init __(* args,** kwargs)
I want to create a slug field stored in database.
I searched and I found http://flask.pocoo.org/snippets/5/ but I'm having trouble integrating the code in my app.
This is my modele.py
:
from unicodedata import normalize
def slugfy(text, encoding=None,
permitted_chars='abcdefghijklmnopqrstuvwxyz0123456789-'):
if isinstance(text, str):
text = text.decode(encoding or 'ascii')
clean_text = text.strip().replace(' ', '-').lower()
while '--' in clean_text:
clean_text = clean_text.replace('--', '-')
ascii_text = normalize('NFKD', clean_text).encode('ascii', 'ignore')
strict_text = map(lambda x: x if x in permitted_chars else '', ascii_text)
return ''.join(strict_text)
class Chanteur(db.Model):
id = db.Column(db.Integer, primary_key = True)
nom = db.Column(db.String(200), index = True, unique = True)
slug = db.Column(db.String(255))
chanteurs = db.relationship('Chanson', backref = 'chanteur', lazy = 'dynamic')
def __repr__(self):
return '<Chanteur %r>' % (self.nom)
def __setattr__(self, key, value):
super(Chanteur, self).__setattr__(key, value)
if key == 'nom':
self.slug = slugfy(self.nom)
class Chanson(db.Model):
id = db.Column(db.Integer, primary_key = True)
titre = db.Column(db.String(255))
chanteur_id = db.Column(db.Integer, db.ForeignKey('chanteur.id'))
def __repr__(self):
return '<Chanson %r>' % (self.titre)
It is not working: when I add a new objet (chanteur) the slug field is empty
解决方案
To persist the slug in the database, I use the following approach (employing the very helpful python-slugify
library):
from slugify import slugify # among other things
class Song(db.Model):
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(255))
slug = db.Column(db.String(255))
def __init__(self, *args, **kwargs):
if not 'slug' in kwargs:
kwargs['slug'] = slugify(kwargs.get('title', ''))
super().__init__(*args, **kwargs)
这篇关于在烧瓶的子弹领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!