Better to save a slug to the DB or generate dynamically?

最后都变了- 提交于 2019-12-04 08:57:40

If you're using the slug for decorative (rather than lookup) purposes, generating it dynamically is the best idea.

Additionally, the code sample you posted can be written like this:

@property
def slug(self):
  return slugify(self.title)

Try making a slug out of the word "café" or "浦安鉄筋家族".

Chances are that it'll look like poo, unless you're really well-prepared.

Sometimes you need the ability to customize slugs.

The downside would be that you're automatically generating the slug every time you render the page. The upside is that you're not taking up space in the database with a field that will never be directly queried against.

Either way is fine, it just depends on your performance vs. space requirements.

The main downside of generating slugs dynamically is that you miss the ability to customize slugs per-object, eg. make them shorter and prettier. For English titles this can be OK, but for non-English content generated slugs can be ugly.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!