Django:如果用于查找,最好将 slug 保存到数据库或动态生成?
所以这几乎是这个问题的重复< /a>,除了我确实想要使用 slug 进行查找。
我的 URL 如下所示:
http://url.com/county/place-name
“县”和“地名”的唯一组合用于查找数据库对象,但“地名”在数据库中存储为“地名”。
因此,如果我不将地名作为单独的 slug 字段存储在数据库中,我需要先对其进行反 slugify,然后才进行查找。
Django 中的 de-slugification 安全/可能吗?或者我最好在数据库中添加一个地名 slug 字段,并在添加新对象时填充它?
So this is almost a duplicate of this question, except that I do want to use the slug for lookup.
My URLs look like this:
http://url.com/county/place-name
The unique combination of 'county' and 'place-name' is used to look up the database object, except that 'place-name' is stored in the database as 'Place Name'.
So if I don't store place-name in the database as a separate slug field, I would need to de-slugify it first, and only then then do lookup.
Is de-slugification safe/possible in Django? Or would I be better off adding a place-name slug field to my database, and populating it whenever a new object gets added?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果用于查找,我总是将其保存到数据库中:应避免损坏的 URL,以防万一您需要更改地名。如果以后您出于任何原因需要更改slugification算法,至少旧数据不会被破坏。等等。
如果您以后需要更改 slugs,您至少可以在保存旧的 slugs 的同时处理迁移。
I'd always save it to the database if it's used for lookup: Broken URLs are to be avoided, just in case you need to change the placename. If later you need for any reason to change the slugification algorithm, at least old data won't break. Etc.
If you need to change slugs later, you can at least deal with the migration while still having the old slugs saved.