コード例 #1
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithUniqueSlugFK(Model):
    """
    >>> sm1 = SimpleModel.objects.create(name='test')
    >>> sm2 = SimpleModel.objects.create(name='test')
    >>> sm3 = SimpleModel.objects.create(name='test2')
    >>> greeting = 'Hello world!'
    >>> a = ModelWithUniqueSlugFK.objects.create(name=greeting, simple_model=sm1)
    >>> a.slug
    u'hello-world'
    >>> b = ModelWithUniqueSlugFK.objects.create(name=greeting, simple_model=sm2)
    >>> b.slug
    u'hello-world-2'
    >>> c = ModelWithUniqueSlugFK.objects.create(name=greeting, simple_model=sm3)
    >>> c.slug
    u'hello-world'
    >>> d = ModelWithUniqueSlugFK.objects.create(name=greeting, simple_model=sm1)
    >>> d.slug
    u'hello-world-3'
    >>> sm3.name = 'test'
    >>> sm3.save()
    >>> c.slug
    u'hello-world'
    >>> c.save()
    >>> c.slug
    u'hello-world-4'
    """
    name = CharField(max_length=200)
    simple_model = ForeignKey(SimpleModel)
    slug = AutoSlugField(populate_from='name',
                         unique_with='simple_model__name')
コード例 #2
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithCustomSeparator(Model):
    """
    >>> a = ModelWithCustomSeparator.objects.create(slug='hello world!')
    >>> b = ModelWithCustomSeparator.objects.create(slug='hello world!')
    >>> b.slug
    u'hello-world_2'
    """
    slug = AutoSlugField(unique=True, sep='_')
コード例 #3
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithCustomSlugifier(Model):
    """
    >>> a = ModelWithCustomSlugifier.objects.create(slug='hello world!')
    >>> b = ModelWithCustomSlugifier.objects.create(slug='hello world!')
    >>> b.slug
    u'hello_world-2'
    """
    slug = AutoSlugField(unique=True, slugify=custom_slugify)
コード例 #4
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class SimpleModel(Model):
    """
    >>> a = SimpleModel(name='test')
    >>> a.save()
    >>> a.slug
    'simplemodel'
    """
    name = CharField(max_length=200)
    slug = AutoSlugField()
コード例 #5
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithAcceptableEmptyDependency(Model):
    """
    >>> model = ModelWithAcceptableEmptyDependency
    >>> instances = [model.objects.create(slug='hello') for x in range(0,2)]
    >>> [x.slug for x in model.objects.all()]
    [u'hello', u'hello-2']
    """
    date = DateField(blank=True, null=True)
    slug = AutoSlugField(unique_with='date')
コード例 #6
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithCallable(Model):
    """
    >>> a = ModelWithCallable.objects.create(name='larch')
    >>> a.slug
    u'the-larch'
    """
    name = CharField(max_length=200)
    slug = AutoSlugField(
        populate_from=lambda instance: u'the %s' % instance.name)
コード例 #7
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithReferenceToItself(Model):
    """
    >>> a = ModelWithReferenceToItself(slug='test')
    >>> a.save()
    Traceback (most recent call last):
    ...
    ValueError: Attribute ModelWithReferenceToItself.slug references itself \
    in `unique_with`. Please use "unique=True" for this case.
    """
    slug = AutoSlugField(unique_with='slug')
コード例 #8
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithWrongReferencedField(Model):
    """
    >>> a = ModelWithWrongReferencedField(slug='test')
    >>> a.save()
    Traceback (most recent call last):
    ...
    ValueError: Could not find attribute ModelWithWrongReferencedField.wrong_field \
    referenced by ModelWithWrongReferencedField.slug (see constraint `unique_with`)
    """
    slug = AutoSlugField(unique_with='wrong_field')
コード例 #9
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithLongName(Model):
    """
    >>> long_name = 'x' * 250
    >>> a = ModelWithLongName(name=long_name)
    >>> a.save()
    >>> len(a.slug)    # original slug is cropped by field length
    50
    """
    name = CharField(max_length=200)
    slug = AutoSlugField(populate_from='name')
コード例 #10
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithWrongLookupInUniqueWith(Model):
    """
    >>> a = ModelWithWrongLookupInUniqueWith(name='test', slug='test')
    >>> a.save()
    Traceback (most recent call last):
    ...
    ValueError: Could not resolve lookup "name__foo" in `unique_with` of \
    ModelWithWrongLookupInUniqueWith.slug
    """
    slug = AutoSlugField(unique_with='name__foo')
    name = CharField(max_length=10)
コード例 #11
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithCallableAttr(Model):
    """
    >>> a = ModelWithCallableAttr.objects.create(name='albatross')
    >>> a.slug
    u'spam-albatross-and-spam'
    """
    name = CharField(max_length=200)
    slug = AutoSlugField(populate_from='get_name')

    def get_name(self):
        return u'spam, %s and spam' % self.name
コード例 #12
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithWrongFieldOrder(Model):
    """
    >>> a = ModelWithWrongFieldOrder(slug='test')
    >>> a.save()
    Traceback (most recent call last):
    ...
    ValueError: Could not check uniqueness of ModelWithWrongFieldOrder.slug with \
    respect to ModelWithWrongFieldOrder.date because the latter is empty. Please \
    ensure that "slug" is declared *after* all fields listed in unique_with.
    """
    slug = AutoSlugField(unique_with='date')
    date = DateField(blank=False, null=False)
コード例 #13
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithAutoUpdateEnabled(Model):
    """
    >>> a = ModelWithAutoUpdateEnabled(name='My name')
    >>> a.save()
    >>> a.slug
    u'my-name'
    >>> a.name = 'My new name'
    >>> a.save()
    >>> a.slug
    u'my-new-name'
    """
    name = CharField(max_length=200)
    slug = AutoSlugField(populate_from='name', always_update=True)
コード例 #14
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithCustomPrimaryKey(Model):
    """
    # just check if models are created without exceptions
    >>> a = ModelWithCustomPrimaryKey.objects.create(custom_primary_key='a',
    ...                                              name='name used in slug')
    >>> b = ModelWithCustomPrimaryKey.objects.create(custom_primary_key='b',
    ...                                              name='name used in slug')
    >>> a.slug
    u'name-used-in-slug'
    """
    custom_primary_key = CharField(primary_key=True, max_length=1)
    name = CharField(max_length=200)
    slug = AutoSlugField(populate_from='name', unique=True)
コード例 #15
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithUniqueSlug(Model):
    """
    >>> greeting = 'Hello world!'
    >>> a = ModelWithUniqueSlug(name=greeting)
    >>> a.save()
    >>> a.slug
    u'hello-world'
    >>> b = ModelWithUniqueSlug(name=greeting)
    >>> b.save()
    >>> b.slug
    u'hello-world-2'
    """
    name = CharField(max_length=200)
    slug = AutoSlugField(populate_from='name', unique=True)
コード例 #16
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithUniqueSlugDay(Model):  # same as ...Date, just more explicit
    """
    >>> a = ModelWithUniqueSlugDay(slug='test', date=datetime.date(2009, 9,  9))
    >>> b = ModelWithUniqueSlugDay(slug='test', date=datetime.date(2009, 9,  9))
    >>> c = ModelWithUniqueSlugDay(slug='test', date=datetime.date(2009, 9, 10))
    >>> for m in a,b,c:
    ...     m.save()
    >>> a.slug
    u'test'
    >>> b.slug
    u'test-2'
    >>> c.slug
    u'test'
    """
    date = DateField()
    slug = AutoSlugField(unique_with='date__day')
コード例 #17
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithUniqueSlugDate(Model):
    """
    >>> a = ModelWithUniqueSlugDate(slug='test', date=datetime.date(2009,9,9))
    >>> b = ModelWithUniqueSlugDate(slug='test', date=datetime.date(2009,9,9))
    >>> c = ModelWithUniqueSlugDate(slug='test', date=datetime.date(2009,9,10))
    >>> for m in a,b,c:
    ...     m.save()
    >>> a.slug
    u'test'
    >>> b.slug
    u'test-2'
    >>> c.slug
    u'test'
    """
    date = DateField()
    slug = AutoSlugField(unique_with='date')
コード例 #18
0
ファイル: tests.py プロジェクト: alexliyu/lincdm
class ModelWithLongNameUnique(Model):
    """
    >>> long_name = 'x' * 250
    >>> a = ModelWithLongNameUnique(name=long_name)
    >>> a.save()
    >>> len(a.slug)    # original slug is cropped by field length
    50
    >>> b = ModelWithLongNameUnique(name=long_name)
    >>> b.save()
    >>> b.slug[-3:]    # uniqueness is forced
    u'x-2'
    >>> len(b.slug)    # slug is cropped
    50
    """
    name = CharField(max_length=200)
    slug = AutoSlugField(populate_from='name', unique=True)