本文整理汇总了Python中jmbo.models.ModelBase.slug方法的典型用法代码示例。如果您正苦于以下问题:Python ModelBase.slug方法的具体用法?Python ModelBase.slug怎么用?Python ModelBase.slug使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jmbo.models.ModelBase
的用法示例。
在下文中一共展示了ModelBase.slug方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_unique_slugs
# 需要导入模块: from jmbo.models import ModelBase [as 别名]
# 或者: from jmbo.models.ModelBase import slug [as 别名]
def test_unique_slugs(self):
# create 2 sites
site_1 = Site(id=201, domain="site1.example.com")
site_1.save()
site_2 = Site(id=202, domain="site2.example.com")
site_2.save()
# Create an object for site 1
obj_1 = ModelBase(title='object for site 1')
obj_1.save()
obj_1.sites.add(site_1)
obj_1.slug = 'generic_slug'
obj_1.save()
# Create an object for site 2
obj_2 = ModelBase(title='object for site 2')
obj_2.save()
obj_2.sites.add(site_2)
obj_2.slug = 'generic_slug'
obj_2.save()
# Trying to add site_1 should raise an error.
with self.assertRaises(RuntimeError):
obj_2.sites.add(site_1)
obj_2.save()
# When the slugs differ, you can add site_1.
obj_2.slug = 'generic_slug_2'
obj_2.sites.add(site_1)
obj_2.save()
# Trying to change the slug to an existing one should raise an error.
with self.assertRaises(RuntimeError):
obj_2.slug = 'generic_slug'
obj_2.save()
示例2: test_unique_slugs
# 需要导入模块: from jmbo.models import ModelBase [as 别名]
# 或者: from jmbo.models.ModelBase import slug [as 别名]
def test_unique_slugs(self):
obj_1 = ModelBase(title="object for site 1")
obj_1.save()
obj_1.sites.add(self.web_site)
obj_1.slug = "generic_slug"
obj_1.save()
# Create an object for site 2
obj_2 = ModelBase(title="object for site 2")
obj_2.save()
obj_2.sites.add(self.mobile_site)
obj_2.slug = "generic_slug"
obj_2.save()
# Trying to add site_1 should raise an error.
with self.assertRaises(IntegrityError):
with transaction.atomic():
obj_2.sites.add(self.web_site)
obj_2.save()
# When the slugs differ, you can add site_1.
obj_2.slug = "generic_slug_2"
obj_2.sites.add(self.web_site)
obj_2.save()
# Trying to change the slug to an existing one should raise an error.
with self.assertRaises(IntegrityError):
with transaction.atomic():
obj_2.slug = "generic_slug"
obj_2.save()