问题:如何在Django中创建子弹?
我正在尝试SlugField
在Django中创建一个。
我创建了这个简单的模型:
from django.db import models
class Test(models.Model):
q = models.CharField(max_length=30)
s = models.SlugField()
然后,我这样做:
>>> from mysite.books.models import Test
>>> t=Test(q="aa a a a", s="b b b b")
>>> t.s
'b b b b'
>>> t.save()
>>> t.s
'b b b b'
我在期待b-b-b-b
。
I am trying to create a SlugField
in Django.
I created this simple model:
from django.db import models
class Test(models.Model):
q = models.CharField(max_length=30)
s = models.SlugField()
I then do this:
>>> from mysite.books.models import Test
>>> t=Test(q="aa a a a", s="b b b b")
>>> t.s
'b b b b'
>>> t.save()
>>> t.s
'b b b b'
I was expecting b-b-b-b
.
回答 0
您将需要使用Slugify函数。
>>> from django.template.defaultfilters import slugify
>>> slugify("b b b b")
u'b-b-b-b'
>>>
您可以slugify
通过覆盖save
方法自动调用:
class Test(models.Model):
q = models.CharField(max_length=30)
s = models.SlugField()
def save(self, *args, **kwargs):
self.s = slugify(self.q)
super(Test, self).save(*args, **kwargs)
请注意,以上内容将导致您在修改q
字段时更改您的URL ,这可能会导致链接断开。创建新对象时最好只生成一次子弹:
class Test(models.Model):
q = models.CharField(max_length=30)
s = models.SlugField()
def save(self, *args, **kwargs):
if not self.id:
# Newly created object, so set slug
self.s = slugify(self.q)
super(Test, self).save(*args, **kwargs)
You will need to use the slugify function.
>>> from django.template.defaultfilters import slugify
>>> slugify("b b b b")
u'b-b-b-b'
>>>
You can call slugify
automatically by overriding the save
method:
class Test(models.Model):
q = models.CharField(max_length=30)
s = models.SlugField()
def save(self, *args, **kwargs):
self.s = slugify(self.q)
super(Test, self).save(*args, **kwargs)
Be aware that the above will cause your URL to change when the q
field is edited, which can cause broken links. It may be preferable to generate the slug only once when you create a new object:
class Test(models.Model):
q = models.CharField(max_length=30)
s = models.SlugField()
def save(self, *args, **kwargs):
if not self.id:
# Newly created object, so set slug
self.s = slugify(self.q)
super(Test, self).save(*args, **kwargs)
回答 1
有一些utf-8字符的特殊情况
例:
>>> from django.template.defaultfilters import slugify
>>> slugify(u"test ąęśćółń")
u'test-aescon' # there is no "l"
这可以用Unidecode解决
>>> from unidecode import unidecode
>>> from django.template.defaultfilters import slugify
>>> slugify(unidecode(u"test ąęśćółń"))
u'test-aescoln'
There is corner case with some utf-8 characters
Example:
>>> from django.template.defaultfilters import slugify
>>> slugify(u"test ąęśćółń")
u'test-aescon' # there is no "l"
This can be solved with Unidecode
>>> from unidecode import unidecode
>>> from django.template.defaultfilters import slugify
>>> slugify(unidecode(u"test ąęśćółń"))
u'test-aescoln'
回答 2
对Thepeer答案的一个小修正:要覆盖save()
模型类中的函数,最好向其添加参数:
from django.utils.text import slugify
def save(self, *args, **kwargs):
if not self.id:
self.s = slugify(self.q)
super(test, self).save(*args, **kwargs)
否则,test.objects.create(q="blah blah blah")
将导致force_insert
错误(意外参数)。
A small correction to Thepeer’s answer: To override save()
function in model classes, better add arguments to it:
from django.utils.text import slugify
def save(self, *args, **kwargs):
if not self.id:
self.s = slugify(self.q)
super(test, self).save(*args, **kwargs)
Otherwise, test.objects.create(q="blah blah blah")
will result in a force_insert
error (unexpected argument).
回答 3
如果您使用的管理界面增加模型的新项目,你可以建立一个ModelAdmin
在你admin.py
和利用prepopulated_fields
自动进入蛞蝓的:
class ClientAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('name',)}
admin.site.register(Client, ClientAdmin)
在这里,当用户在admin表单中为该name
字段输入值时,slug
将使用正确的slugified自动填充name
。
If you’re using the admin interface to add new items of your model, you can set up a ModelAdmin
in your admin.py
and utilize prepopulated_fields
to automate entering of a slug:
class ClientAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('name',)}
admin.site.register(Client, ClientAdmin)
Here, when the user enters a value in the admin form for the name
field, the slug
will be automatically populated with the correct slugified name
.
回答 4
在大多数情况下,该条块不应更改,因此您实际上只想在首次保存时进行计算:
class Test(models.Model):
q = models.CharField(max_length=30)
s = models.SlugField(editable=False) # hide from admin
def save(self):
if not self.id:
self.s = slugify(self.q)
super(Test, self).save()
In most cases the slug should not change, so you really only want to calculate it on first save:
class Test(models.Model):
q = models.CharField(max_length=30)
s = models.SlugField(editable=False) # hide from admin
def save(self):
if not self.id:
self.s = slugify(self.q)
super(Test, self).save()
回答 5
使用prepopulated_fields
在您的管理类:
class ArticleAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
admin.site.register(Article, ArticleAdmin)
Use prepopulated_fields
in your admin class:
class ArticleAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
admin.site.register(Article, ArticleAdmin)
回答 6
如果您不想将slugfield设置为“不可编辑”,那么我相信您希望将Null和Blank属性设置为False。否则,尝试保存到Admin时会收到错误消息。
因此,对上述示例的修改如下:
class test(models.Model):
q = models.CharField(max_length=30)
s = models.SlugField(null=True, blank=True) # Allow blank submission in admin.
def save(self):
if not self.id:
self.s = slugify(self.q)
super(test, self).save()
If you don’t want to set the slugfield to Not be editable, then I believe you’ll want to set the Null and Blank properties to False. Otherwise you’ll get an error when trying to save in Admin.
So a modification to the above example would be::
class test(models.Model):
q = models.CharField(max_length=30)
s = models.SlugField(null=True, blank=True) # Allow blank submission in admin.
def save(self):
if not self.id:
self.s = slugify(self.q)
super(test, self).save()
回答 7
我正在使用Django 1.7
像这样在模型中创建一个SlugField:
slug = models.SlugField()
然后在admin.py
定义prepopulated_fields
;
class ArticleAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
I’m using Django 1.7
Create a SlugField in your model like this:
slug = models.SlugField()
Then in admin.py
define prepopulated_fields
;
class ArticleAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}
回答 8