有没有简单的方法从CharField填充SlugField? [英] Is there an easy way to populate SlugField from CharField?

查看:79
本文介绍了有没有简单的方法从CharField填充SlugField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ f $($ model $ M $)
title = models.CharField(max_length = 20)
slug = models.SlugField()

是否有一种内置的方式来根据标题自动填充lug lug字段?也许在管理员和管理员之外。

解决方案

在Django 1.0及更高版本中,您需要使用

  prepopulated_fields = {'slug':('title',),} 
/ pre>

在您的admin.py



您预填充字段字典中的键是您要填写的字段,并且该值是要连接的字段的元组。



在管理员之外,您可以使用 slugify 函数在你的意见在模板中,您可以使用 | slugify 过滤器。



还有这个包将照顾这个自动: https://pypi.python.org/pypi/django-autoslug


class Foo(models.Model):
    title = models.CharField(max_length=20)
    slug = models.SlugField()

Is there a built-in way to get the slug field to autopopulate based on the title? Perhaps in the Admin and outside of the Admin.

解决方案

for Admin in Django 1.0 and up, you'd need to use

prepopulated_fields = {'slug': ('title',), }

in your admin.py

Your key in the prepopulated_fields dictionary is the field you want filled, and the value is a tuple of fields you want concatenated.

Outside of admin, you can use the slugify function in your views. In templates, you can use the |slugify filter.

There is also this package which will take care of this automatically: https://pypi.python.org/pypi/django-autoslug

这篇关于有没有简单的方法从CharField填充SlugField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆