问题描述
我试图在Django中创建一个独特的s o,以便我可以通过这样的URL访问一个帖子:
相关模型:
类ZipCode(models.Model):
zipcode = models.CharField max_length = 5)
city = models.CharField(max_length = 64)
statecode = models.CharField(max_length = 32)
类需要(models.Model):
title = models.CharField(max_length = 50)
us_zip = models.CharField(max_length = 5)
slug =
def get_city():
zip = ZipCode.objects.get(zipcode = self.us_zip)
city =%s,%s%s%(zip。城市,zip.statecode,zip.zipcode)
返回城市
示例ZipCode记录:
- zipcode =02111
- city =Boston
- statecode =MA
一个示例需求记录:
- title =买新自行车
- us_zip =02111
- slug =buy-a -new-bike_Boston-MA-02111_2(需要)
有关如何创建这个独特的s>的提示?它的组成是:
- Need.title +_+ Need.get_city()+_+一个可选的递增整数它独特所有的空格都应该被替换为 - 。
注意:自行车-Boston-MA-02111已经存在,这是它附加了_2,使其独一无二。
我尝试过django扩展,但是似乎只能采取领域或元组的领域来构建独特的s。。我需要传递get_city()函数以及标题和城市之间的_连接器。任何人解决了这个并愿意分享?
谢谢!
更新 / p>
我已经在其UUIDField中使用django扩展,所以如果它也可以用于其AutoSlugField,那将是很好的!
我使用这个来生成
slug将是Django SlugField,其中blank = True,但在save方法中执行slug。
需要模型的典型保存方法可能在下面
def save(self,** kwargs):
slug_str =%s%s%(self.title,self.us_zip)
unique_slugify(self,slug_str)
超级(需要,自).save(** kwargs)
,这样会产生一个sㄧlike like lug lug bike on on on on on--------- -bike_Boston-MA-02111-1等。输出可能有所不同,但您可以随时浏览片段并自定义您的需求。
I am trying to create a unique slug in Django so that I can access a post via a url like this:http://www.example.com/buy-a-new-bike_Boston-MA-02111_2
The relevant models:
class ZipCode(models.Model):
zipcode = models.CharField(max_length=5)
city = models.CharField(max_length=64)
statecode = models.CharField(max_length=32)
class Need(models.Model):
title = models.CharField(max_length=50)
us_zip = models.CharField(max_length=5)
slug = ?????
def get_city():
zip = ZipCode.objects.get(zipcode=self.us_zip)
city = "%s, %s %s" % (zip.city, zip.statecode, zip.zipcode)
return city
A sample ZipCode record:
- zipcode = "02111"
- city = "Boston"
- statecode = "MA"
A sample Need record:
- title = "buy a new bike"
- us_zip = "02111"
- slug = "buy-a-new-bike_Boston-MA-02111_2" (desired)
Any tips as to how to create this unique slug? Its composition is:
- Need.title + "_" + Need.get_city() + "_" + an optional incrementing integer to make it unique. All spaces should be replaced with "-".
NOTE: My desired slug above assumes that the slug "buy-a-new-bike_Boston-MA-02111" already exists, which is what it has the "_2" appended to it to make it unique.
I've tried django-extensions, but it seems that it can only take a field or tuple of fields to construct the unique slug. I need to pass in the get_city() function as well as the "_" connector between the title and city. Anyone solved this and willing to share?
Thank you!
UPDATE
I'm already using django-extensions for its UUIDField, so it would be nice if it could also be usable for its AutoSlugField!
I use this snippet for generating unique slug and my typical save method look like below
slug will be Django SlugField with blank=True but enforce slug in save method.
typical save method for Need model might look below
def save(self, **kwargs):
slug_str = "%s %s" % (self.title, self.us_zip)
unique_slugify(self, slug_str)
super(Need, self).save(**kwargs)
and this will generate slug like buy-a-new-bike_Boston-MA-02111 , buy-a-new-bike_Boston-MA-02111-1 and so on. Output might be little different but you can always go through snippet and customize to your needs.
这篇关于如何在Django中创建一个独特的插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!