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
相关模式:
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
样本ZipCode记录:
- zipcode = "02111"
- city = "Boston"
- statecode = "MA"
需求记录样本:
- title = "buy a new bike"
- us_zip = "02111"
- slug = "buy-a-new-bike_Boston-MA-02111_2" (desired)
如何创造这一独特lu? 其组成如下:
- Need.title + "_" + Need.get_city() + "_" + an optional incrementing integer to make it unique. All spaces should be replaced with "-".
注:我上文所希望的概要假设,“Boston-MA-02111”的 s鱼已经存在,这是它为了使其独一无二而附加的“_2”。
我曾尝试过暴风雨,但似乎只能从田地或田径修建独一无二的lu。 我需要在所有权和城市之间的“城市”功能以及“_”连接器上通过。 任何人都解决这个问题,愿意分享?
谢谢!
<>>>>>
我已经为其“统一调查”现场使用过 d沟,因此,如果它也能为其“自动调查”现场使用的话,那将是ice!